mirror of
https://github.com/boostorg/array.git
synced 2025-07-19 23:52:08 +02:00
Support for zero length arrays
[SVN r34154]
This commit is contained in:
107
array0.cpp
Normal file
107
array0.cpp
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
/* tests for using class array<> specialization for size 0
|
||||||
|
* (C) Copyright Alisdair Meredith 2006.
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See
|
||||||
|
* accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
* http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <boost/array.hpp>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
unsigned int failed_tests = 0;
|
||||||
|
|
||||||
|
void fail_test( const char * reason ) {
|
||||||
|
++failed_tests;
|
||||||
|
std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
template< class T >
|
||||||
|
void BadValue( const T & )
|
||||||
|
{
|
||||||
|
fail_test( "Unexpected value" );
|
||||||
|
}
|
||||||
|
|
||||||
|
template< class T >
|
||||||
|
void RunTests()
|
||||||
|
{
|
||||||
|
typedef boost::array< T, 0 > test_type;
|
||||||
|
|
||||||
|
// Test value and aggegrate initialization
|
||||||
|
test_type test_case = {};
|
||||||
|
const boost::array< T, 0 > const_test_case = test_type();
|
||||||
|
|
||||||
|
test_case.assign( T() );
|
||||||
|
|
||||||
|
// front/back and operator[] must compile, but calling them is undefined
|
||||||
|
// Likewise, all tests below should evaluate to false, avoiding undefined behaviour
|
||||||
|
if( !test_case.empty() ) {
|
||||||
|
BadValue( test_case.front() );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( !const_test_case.empty() ) {
|
||||||
|
BadValue( const_test_case.back() );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( test_case.size() > 0 ) {
|
||||||
|
BadValue( test_case[ 0 ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( const_test_case.max_size() > 0 ) {
|
||||||
|
BadValue( const_test_case[ 0 ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert requirements of TR1 6.2.2.4
|
||||||
|
if( test_case.begin() != test_case.end() ) {
|
||||||
|
fail_test( "Not an empty range" );
|
||||||
|
}
|
||||||
|
if( const_test_case.begin() != const_test_case.end() ) {
|
||||||
|
fail_test( "Not an empty range" );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( test_case.begin() == const_test_case.begin() ) {
|
||||||
|
fail_test( "iterators for different containers are not distinct" );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( test_case.data() == const_test_case.data() ) {
|
||||||
|
// Value of data is unspecified in TR1, so no requirement this test pass or fail
|
||||||
|
// However, it must compile!
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Check can safely use all iterator types with std algorithms
|
||||||
|
std::for_each( test_case.begin(), test_case.end(), BadValue< T > );
|
||||||
|
std::for_each( test_case.rbegin(), test_case.rend(), BadValue< T > );
|
||||||
|
std::for_each( const_test_case.begin(), const_test_case.end(), BadValue< T > );
|
||||||
|
std::for_each( const_test_case.rbegin(), const_test_case.rend(), BadValue< T > );
|
||||||
|
|
||||||
|
// Check swap is well formed
|
||||||
|
std::swap( test_case, test_case );
|
||||||
|
|
||||||
|
// Check assigment operator and overloads are well formed
|
||||||
|
test_case = const_test_case;
|
||||||
|
|
||||||
|
// Confirm at() throws the std lib defined exception
|
||||||
|
try {
|
||||||
|
BadValue( test_case.at( 0 ) );
|
||||||
|
} catch ( const std::range_error & ) {
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
BadValue( const_test_case.at( 0 ) );
|
||||||
|
} catch ( const std::range_error & ) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
RunTests< bool >();
|
||||||
|
RunTests< void * >();
|
||||||
|
RunTests< long double >();
|
||||||
|
RunTests< std::string >();
|
||||||
|
return failed_tests;
|
||||||
|
}
|
||||||
|
|
@ -135,6 +135,7 @@ namespace boost {
|
|||||||
|
|
||||||
// direct access to data (read-only)
|
// direct access to data (read-only)
|
||||||
const T* data() const { return elems; }
|
const T* data() const { return elems; }
|
||||||
|
T* data() { return elems; }
|
||||||
|
|
||||||
// use array as C array (direct read/write access to data)
|
// use array as C array (direct read/write access to data)
|
||||||
T* c_array() { return elems; }
|
T* c_array() { return elems; }
|
||||||
@ -161,6 +162,120 @@ namespace boost {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template< class T >
|
||||||
|
class array< T, 0 > {
|
||||||
|
|
||||||
|
public:
|
||||||
|
// type definitions
|
||||||
|
typedef T value_type;
|
||||||
|
typedef T* iterator;
|
||||||
|
typedef const T* const_iterator;
|
||||||
|
typedef T& reference;
|
||||||
|
typedef const T& const_reference;
|
||||||
|
typedef std::size_t size_type;
|
||||||
|
typedef std::ptrdiff_t difference_type;
|
||||||
|
|
||||||
|
// iterator support
|
||||||
|
iterator begin() { return iterator( reinterpret_cast< T * >( this ) ); }
|
||||||
|
const_iterator begin() const { return const_iterator( reinterpret_cast< const T * >( this ) ); }
|
||||||
|
iterator end() { return begin(); }
|
||||||
|
const_iterator end() const { return begin(); }
|
||||||
|
|
||||||
|
// reverse iterator support
|
||||||
|
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
|
||||||
|
typedef std::reverse_iterator<iterator> reverse_iterator;
|
||||||
|
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||||
|
#elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
|
||||||
|
// workaround for broken reverse_iterator in VC7
|
||||||
|
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
|
||||||
|
reference, iterator, reference> > reverse_iterator;
|
||||||
|
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
|
||||||
|
const_reference, iterator, reference> > const_reverse_iterator;
|
||||||
|
#else
|
||||||
|
// workaround for broken reverse_iterator implementations
|
||||||
|
typedef std::reverse_iterator<iterator,T> reverse_iterator;
|
||||||
|
typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
reverse_iterator rbegin() { return reverse_iterator(end()); }
|
||||||
|
const_reverse_iterator rbegin() const {
|
||||||
|
return const_reverse_iterator(end());
|
||||||
|
}
|
||||||
|
reverse_iterator rend() { return reverse_iterator(begin()); }
|
||||||
|
const_reverse_iterator rend() const {
|
||||||
|
return const_reverse_iterator(begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
// operator[]
|
||||||
|
reference operator[](size_type i)
|
||||||
|
{
|
||||||
|
BOOST_ASSERT( "out of range" );
|
||||||
|
failed_rangecheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reference operator[](size_type i) const
|
||||||
|
{
|
||||||
|
BOOST_ASSERT( "out of range" );
|
||||||
|
failed_rangecheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
// at() with range check
|
||||||
|
reference at(size_type i) { failed_rangecheck(); }
|
||||||
|
const_reference at(size_type i) const { failed_rangecheck(); }
|
||||||
|
|
||||||
|
// front() and back()
|
||||||
|
reference front()
|
||||||
|
{
|
||||||
|
failed_rangecheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reference front() const
|
||||||
|
{
|
||||||
|
failed_rangecheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
reference back()
|
||||||
|
{
|
||||||
|
failed_rangecheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reference back() const
|
||||||
|
{
|
||||||
|
failed_rangecheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
// size is constant
|
||||||
|
static size_type size() { return 0; }
|
||||||
|
static bool empty() { return true; }
|
||||||
|
static size_type max_size() { return 0; }
|
||||||
|
enum { static_size = 0 };
|
||||||
|
|
||||||
|
void swap (array<T,0>& y) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// direct access to data (read-only)
|
||||||
|
const T* data() const { return 0; }
|
||||||
|
T* data() { return 0; }
|
||||||
|
|
||||||
|
// use array as C array (direct read/write access to data)
|
||||||
|
T* c_array() { return 0; }
|
||||||
|
|
||||||
|
// assignment with type conversion
|
||||||
|
template <typename T2>
|
||||||
|
array<T,0>& operator= (const array<T2,0>& ) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// assign one value to all elements
|
||||||
|
void assign (const T& ) { }
|
||||||
|
|
||||||
|
// check range (may be private because it is static)
|
||||||
|
static void failed_rangecheck () {
|
||||||
|
throw std::range_error("attempt to access element of an empty array");
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
// comparisons
|
// comparisons
|
||||||
template<class T, std::size_t N>
|
template<class T, std::size_t N>
|
||||||
bool operator== (const array<T,N>& x, const array<T,N>& y) {
|
bool operator== (const array<T,N>& x, const array<T,N>& y) {
|
||||||
|
Reference in New Issue
Block a user