Skip to content

Commit daeb19f

Browse files
committed
Add support for std::get<> to Boost.Array
[SVN r82083]
1 parent f41b1d2 commit daeb19f

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

include/boost/array.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <cstddef>
4343
#include <stdexcept>
4444
#include <boost/assert.hpp>
45+
#include <boost/static_assert.hpp>
4546
#include <boost/swap.hpp>
4647

4748
// Handles broken standard libraries better than <iterator>
@@ -438,6 +439,22 @@ namespace boost {
438439

439440
} /* namespace boost */
440441

442+
#ifndef BOOST_NO_CXX11_HDR_ARRAY
443+
// If we don't have std::array, I'm assuming that we don't have std::get
444+
namespace std {
445+
template <size_t Idx, typename T, size_t N>
446+
T &get(boost::array<T,N> &arr) BOOST_NOEXCEPT {
447+
BOOST_STATIC_ASSERT_MSG ( Idx < N, "std::get<>(boost::array &) index out of range" );
448+
return arr[Idx];
449+
}
450+
451+
template <size_t Idx, typename T, size_t N>
452+
const T &get(const boost::array<T,N> &arr) BOOST_NOEXCEPT {
453+
BOOST_STATIC_ASSERT_MSG ( Idx < N, "std::get<>(const boost::array &) index out of range" );
454+
return arr[Idx];
455+
}
456+
}
457+
#endif
441458

442459
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
443460
# pragma warning(pop)

test/Jamfile.v2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ test-suite array :
1212
[ run array4.cpp ]
1313
[ run array5.cpp ]
1414
[ run array6.cpp ]
15+
[ run array7.cpp ]
1516
[ run array_hash.cpp ]
1617
;

test/array7.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* tests for using class array<> specialization for size 0
2+
* (C) Copyright Alisdair Meredith 2006.
3+
* Distributed under the Boost Software License, Version 1.0. (See
4+
* accompanying file LICENSE_1_0.txt or copy at
5+
* http://www.boost.org/LICENSE_1_0.txt)
6+
*/
7+
8+
#include <string>
9+
#include <iostream>
10+
#include <boost/array.hpp>
11+
#include <algorithm>
12+
#ifndef BOOST_NO_CXX11_HDR_ARRAY
13+
#include <array>
14+
#endif
15+
16+
namespace {
17+
unsigned int failed_tests = 0;
18+
19+
#ifndef BOOST_NO_CXX11_HDR_ARRAY
20+
void fail_test( const char * reason ) {
21+
++failed_tests;
22+
std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl;
23+
}
24+
25+
template< class T >
26+
void RunTests()
27+
{
28+
typedef boost::array< T, 5 > test_type;
29+
typedef T arr[5];
30+
test_type test_case; // = { 1, 1, 2, 3, 5 };
31+
32+
T &aRef = std::get<0> ( test_case );
33+
if ( &*test_case.begin () != &aRef )
34+
fail_test ( "Array7: Same thing not equal?(1)" );
35+
36+
const T &caRef = std::get<0> ( test_case );
37+
if ( &*test_case.cbegin () != &caRef )
38+
fail_test ( "Array7: Same thing not equal?(2)" );
39+
}
40+
#endif
41+
42+
}
43+
44+
int main()
45+
{
46+
#ifndef BOOST_NO_CXX11_HDR_ARRAY
47+
RunTests< bool >();
48+
RunTests< void * >();
49+
RunTests< long double >();
50+
RunTests< std::string >();
51+
#endif
52+
return failed_tests;
53+
}
54+

0 commit comments

Comments
 (0)