MSVC version of get<N> can now handle const tuples properly

Testcase for const tuples added


[SVN r10986]
This commit is contained in:
Douglas Gregor
2001-09-01 01:52:14 +00:00
parent 3bbc493fc1
commit b9cb417106
2 changed files with 33 additions and 21 deletions

View File

@ -264,18 +264,18 @@ namespace boost {
template<int N> template<int N>
struct element struct element
{ {
template<typename Tuple> template<typename Head, typename Tail>
static inline static inline
typename detail::tuples::tuple_element_ref<N, Tuple>::RET typename detail::tuples::tuple_element_ref<N, cons<Head, Tail> >::RET
get(Tuple& t) get(cons<Head, Tail>& t)
{ {
return element<N-1>::get(t.tail); return element<N-1>::get(t.tail);
} }
template<typename Tuple> template<typename Head, typename Tail>
static inline static inline
typename detail::tuples::tuple_element_const_ref<N, Tuple>::RET typename detail::tuples::tuple_element_const_ref<N, cons<Head, Tail> >::RET
get(const Tuple& t) get(const cons<Head, Tail>& t)
{ {
return element<N-1>::get(t.tail); return element<N-1>::get(t.tail);
} }
@ -284,18 +284,18 @@ namespace boost {
template<> template<>
struct element<0> struct element<0>
{ {
template<typename Tuple> template<typename Head, typename Tail>
static inline static inline
typename add_reference<typename Tuple::head_type>::type typename add_reference<Head>::type
get(Tuple& t) get(cons<Head, Tail>& t)
{ {
return t.head; return t.head;
} }
template<typename Tuple> template<typename Head, typename Tail>
static inline static inline
typename add_reference<const typename Tuple::head_type>::type typename add_reference<const Head>::type
get(const Tuple& t) get(const cons<Head, Tail>& t)
{ {
return t.head; return t.head;
} }
@ -361,18 +361,21 @@ namespace boost {
} }
}; };
// Retrieve the Nth element in the typle namespace detail {
template<int N, typename Tuple> namespace tuples {
typename detail::tuples::tuple_element_ref<N, Tuple>::RET template<int N> struct workaround_holder {};
get(Tuple& t) }}
template<int N, typename Head, typename Tail>
typename detail::tuples::tuple_element_ref<N, cons<Head, Tail> >::RET
get(cons<Head, Tail>& t, detail::tuples::workaround_holder<N>* = 0)
{ {
return element<N>::get(t); return element<N>::get(t);
} }
// Retrieve the Nth element in the typle template<int N, typename Head, typename Tail>
template<int N, typename Tuple> typename detail::tuples::tuple_element_const_ref<N, cons<Head, Tail> >::RET
typename detail::tuples::tuple_element_const_ref<N, Tuple>::RET get(const cons<Head, Tail>& t, detail::tuples::workaround_holder<N>* = 0)
get(const Tuple& t)
{ {
return element<N>::get(t); return element<N>::get(t);
} }

View File

@ -273,6 +273,14 @@ void foo8()
#endif #endif
} }
// Testing const tuples
void foo9()
{
const tuple<int, float> t1(5, 3.3f);
BOOST_TEST(get<0>(t1) == 5);
BOOST_TEST(get<1>(t1) == 3.3f);
}
// -------------------------------- // --------------------------------
// ---------------------------- // ----------------------------
@ -286,6 +294,7 @@ int test_main(int, char *[]) {
foo6(); foo6();
foo7(); foo7();
foo8(); foo8();
foo9();
return 0; return 0;
} }