Add boost::get<> support to Boost.Array

[SVN r82089]
This commit is contained in:
Marshall Clow
2012-12-19 00:53:31 +00:00
parent daeb19f693
commit e02e7dcc00
7 changed files with 206 additions and 76 deletions

View File

@ -437,10 +437,22 @@ namespace boost {
return boost::hash_range(arr.begin(), arr.end()); return boost::hash_range(arr.begin(), arr.end());
} }
template <size_t Idx, typename T, size_t N>
T &get(boost::array<T,N> &arr) BOOST_NOEXCEPT {
BOOST_STATIC_ASSERT_MSG ( Idx < N, "boost::get<>(boost::array &) index out of range" );
return arr[Idx];
}
template <size_t Idx, typename T, size_t N>
const T &get(const boost::array<T,N> &arr) BOOST_NOEXCEPT {
BOOST_STATIC_ASSERT_MSG ( Idx < N, "boost::get<>(const boost::array &) index out of range" );
return arr[Idx];
}
} /* namespace boost */ } /* namespace boost */
#ifndef BOOST_NO_CXX11_HDR_ARRAY #ifndef BOOST_NO_CXX11_HDR_ARRAY
// If we don't have std::array, I'm assuming that we don't have std::get // If we don't have std::array, I'm assuming that we don't have std::get
namespace std { namespace std {
template <size_t Idx, typename T, size_t N> template <size_t Idx, typename T, size_t N>
T &get(boost::array<T,N> &arr) BOOST_NOEXCEPT { T &get(boost::array<T,N> &arr) BOOST_NOEXCEPT {

View File

@ -13,5 +13,7 @@ test-suite array :
[ run array5.cpp ] [ run array5.cpp ]
[ run array6.cpp ] [ run array6.cpp ]
[ run array7.cpp ] [ run array7.cpp ]
[ compile-fail array_getfail1.cpp ]
[ compile-fail array_getfail2.cpp ]
[ run array_hash.cpp ] [ run array_hash.cpp ]
; ;

View File

@ -10,39 +10,32 @@
#include <boost/array.hpp> #include <boost/array.hpp>
#include <algorithm> #include <algorithm>
#include <boost/test/included/test_exec_monitor.hpp>
namespace { namespace {
unsigned int failed_tests = 0; template< class T >
void RunTests()
void fail_test( const char * reason ) { {
++failed_tests; typedef boost::array< T, 5 > test_type;
std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl; typedef T arr[5];
} test_type test_case; // = { 1, 1, 2, 3, 5 };
template< class T >
void RunTests()
{
typedef boost::array< T, 5 > test_type;
typedef T arr[5];
test_type test_case; // = { 1, 1, 2, 3, 5 };
arr &aRef = get_c_array ( test_case ); arr &aRef = get_c_array ( test_case );
if ( &*test_case.begin () != &aRef[0] ) BOOST_CHECK ( &*test_case.begin () == &aRef[0] );
fail_test ( "Array6: Same thing not equal?(1)" );
const arr &caRef = get_c_array ( test_case ); const arr &caRef = get_c_array ( test_case );
typename test_type::const_iterator iter = test_case.begin (); typename test_type::const_iterator iter = test_case.begin ();
if ( &*iter != &caRef[0] ) BOOST_CHECK ( &*iter == &caRef[0] );
fail_test ( "Array6: Same thing not equal?(2)" ); }
} }
} int test_main( int , char* [] )
int main()
{ {
RunTests< bool >(); RunTests< bool >();
RunTests< void * >(); RunTests< void * >();
RunTests< long double >(); RunTests< long double >();
RunTests< std::string >(); RunTests< std::string >();
return failed_tests; return 0;
} }

View File

@ -1,5 +1,5 @@
/* tests for using class array<> specialization for size 0 /* tests using std::get on boost:array
* (C) Copyright Alisdair Meredith 2006. * (C) Copyright Marshall Clow 2012
* Distributed under the Boost Software License, Version 1.0. (See * Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at * accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt) * http://www.boost.org/LICENSE_1_0.txt)
@ -13,42 +13,55 @@
#include <array> #include <array>
#endif #endif
#include <boost/test/included/test_exec_monitor.hpp>
namespace { namespace {
unsigned int failed_tests = 0;
#ifndef BOOST_NO_CXX11_HDR_ARRAY #ifndef BOOST_NO_CXX11_HDR_ARRAY
void fail_test( const char * reason ) { template< class T >
++failed_tests; void RunStdTests()
std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl; {
} typedef boost::array< T, 5 > test_type;
typedef T arr[5];
template< class T > test_type test_case; // = { 1, 1, 2, 3, 5 };
void RunTests()
{
typedef boost::array< T, 5 > test_type;
typedef T arr[5];
test_type test_case; // = { 1, 1, 2, 3, 5 };
T &aRef = std::get<0> ( test_case ); T &aRef = std::get<0> ( test_case );
if ( &*test_case.begin () != &aRef ) BOOST_CHECK ( &*test_case.begin () == &aRef );
fail_test ( "Array7: Same thing not equal?(1)" );
const T &caRef = std::get<0> ( test_case ); const T &caRef = std::get<0> ( test_case );
if ( &*test_case.cbegin () != &caRef ) BOOST_CHECK ( &*test_case.cbegin () == &caRef );
fail_test ( "Array7: Same thing not equal?(2)" ); }
} #endif
#endif
template< class T >
void RunBoostTests()
{
typedef boost::array< T, 5 > test_type;
typedef T arr[5];
test_type test_case; // = { 1, 1, 2, 3, 5 };
T &aRef = boost::get<0> ( test_case );
BOOST_CHECK ( &*test_case.begin () == &aRef );
const T &caRef = boost::get<0> ( test_case );
BOOST_CHECK ( &*test_case.cbegin () == &caRef );
}
} }
int main() int test_main( int , char* [] )
{ {
RunBoostTests< bool >();
RunBoostTests< void * >();
RunBoostTests< long double >();
RunBoostTests< std::string >();
#ifndef BOOST_NO_CXX11_HDR_ARRAY #ifndef BOOST_NO_CXX11_HDR_ARRAY
RunTests< bool >(); RunStdTests< bool >();
RunTests< void * >(); RunStdTests< void * >();
RunTests< long double >(); RunStdTests< long double >();
RunTests< std::string >(); RunStdTests< std::string >();
#endif #endif
return failed_tests; return 0;
} }

51
test/array_getfail1.cpp Normal file
View File

@ -0,0 +1,51 @@
/* tests using std::get on boost:array
* (C) Copyright Marshall Clow 2012
* 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 <algorithm>
#ifndef BOOST_NO_CXX11_HDR_ARRAY
#include <array>
#endif
#include <boost/array.hpp>
#include <boost/static_assert.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
namespace {
#ifndef BOOST_NO_CXX11_HDR_ARRAY
template< class T >
void RunStdTests()
{
typedef boost::array< T, 5 > test_type;
typedef T arr[5];
test_type test_case; // = { 1, 1, 2, 3, 5 };
T &aRef = std::get<5> ( test_case ); // should fail to compile
BOOST_CHECK ( &*test_case.begin () == &aRef );
}
#endif
}
int test_main( int , char* [] )
{
#ifndef BOOST_NO_CXX11_HDR_ARRAY
RunStdTests< bool >();
RunStdTests< void * >();
RunStdTests< long double >();
RunStdTests< std::string >();
#else
BOOST_STATIC_ASSERT ( false ); // fail on C++03 systems.
#endif
return 0;
}

64
test/array_getfail2.cpp Normal file
View File

@ -0,0 +1,64 @@
/* tests using std::get on boost:array
* (C) Copyright Marshall Clow 2012
* 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>
#include <algorithm>
#ifndef BOOST_NO_CXX11_HDR_ARRAY
#include <array>
#endif
#include <boost/test/included/test_exec_monitor.hpp>
namespace {
#ifndef BOOST_NO_CXX11_HDR_ARRAY
template< class T >
void RunStdTests()
{
typedef boost::array< T, 5 > test_type;
typedef T arr[5];
test_type test_case; // = { 1, 1, 2, 3, 5 };
T &aRef = std::get<0> ( test_case );
BOOST_CHECK ( &*test_case.begin () == &aRef );
const T &caRef = std::get<0> ( test_case );
BOOST_CHECK ( &*test_case.cbegin () == &caRef );
}
#endif
template< class T >
void RunBoostTests()
{
typedef boost::array< T, 5 > test_type;
typedef T arr[5];
test_type test_case; // = { 1, 1, 2, 3, 5 };
T &aRef = boost::get<5> ( test_case );
BOOST_CHECK ( &*test_case.begin () == &aRef );
}
}
int test_main( int , char* [] )
{
RunBoostTests< bool >();
RunBoostTests< void * >();
RunBoostTests< long double >();
RunBoostTests< std::string >();
#ifndef BOOST_NO_CXX11_HDR_ARRAY
RunStdTests< bool >();
RunStdTests< void * >();
RunStdTests< long double >();
RunStdTests< std::string >();
#endif
return 0;
}

View File

@ -11,39 +11,34 @@
#include <algorithm> #include <algorithm>
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
namespace { namespace {
unsigned int failed_tests = 0;
void fail_test( const char * reason ) { template< class T >
++failed_tests; void RunTests()
std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl; {
} // std::size_t hash0 = boost::hash<boost::array<T,0> > () ( boost::array<T, 0> ());
// std::size_t hash1 = boost::hash<boost::array<T,1> > () ( boost::array<T, 1> ());
template< class T >
void RunTests()
{
// std::size_t hash0 = boost::hash<boost::array<T,0> > () ( boost::array<T, 0> ());
// std::size_t hash1 = boost::hash<boost::array<T,1> > () ( boost::array<T, 1> ());
typedef boost::array< T, 5 > barr; typedef boost::array< T, 5 > barr;
typedef T arr[5]; typedef T arr[5];
barr test_barr = {{ 1, 1, 2, 3, 5 }}; barr test_barr = {{ 1, 1, 2, 3, 5 }};
arr test_arr = { 1, 1, 2, 3, 5 }; arr test_arr = { 1, 1, 2, 3, 5 };
std::size_t bhash = boost::hash<barr> () ( test_barr ); std::size_t bhash = boost::hash<barr> () ( test_barr );
std::size_t ahash = boost::hash<arr> () ( test_arr ); std::size_t ahash = boost::hash<arr> () ( test_arr );
if ( ahash != bhash ) BOOST_CHECK ( ahash == bhash );
fail_test ( "Array_hash: Hash-mismatch on " ); }
}
} }
int main() int test_main( int , char* [] )
{ {
RunTests< int >(); RunTests< int >();
RunTests< long >(); RunTests< long >();
RunTests< long double >(); RunTests< long double >();
return failed_tests; return 0;
} }