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

View File

@ -273,6 +273,14 @@ void foo8()
#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();
foo7();
foo8();
foo9();
return 0;
}