Hi, all.
boost::tuple provide a member template function get to gain a element of
tuple, As following:
#include "boost\tuple\tuple.hpp"
using namespace boost;
template<typename T>
class TA{public:
template
void testfun(T1 e1, T2 e2)
{
tuple t1(e1, e2);
//T1 _e1 = t1.template get<0>(); //works
*T1 _e1 = t1.get<0>(); //works for boost::tuple*
/*
However, according c++98, t1.get<0>() seems to should be encounter a
compilor error.
*I want to known how boost::tuple avoid this compile error.*
*/
}
};
int main()
{
int i=10;
float f=10.5f;
TA<char> obj;
obj.testfun(i, f);
return 0;
}
The above sample pass the compilation of gcc3.4.2 and gcc 4.2.4.
*According to boost::tuple, I implement a new tuple by myself. as following:
*
(In fact, the following code is mostly extracted from boost::tuple. )
namespace testNS{
template
struct element
{
private:
typedef typename T::tail_type Next;
public:
typedef typename element::type type;
};
template<class T>
struct element<0,T>
{
typedef typename T::head_type type;
};
struct null_type{};
template
struct cons
{
typedef HT head_type;
typedef TT tail_type;
template <int N>
typename element >::type
*get()* {
typedef typename element >::type RetType;
return RetType();
}
};
template<typename HT>
struct cons
{
typedef HT head_type;
typedef null_type tail_type;
typedef cons self_type;
//get(BOOST_EXPLICIT_TEMPLATE_NON_TYPE(int, N))
template <int N>
typename element::type
*get()* {
typedef typename element >::type RetType;
return RetType();
//return boost::tuples::get<N>(*this);
}
};
template
struct map_tuple_to_cons
{
typedef cons::type
> type;
};
// The empty tuple is a null_type
template <>
struct map_tuple_to_cons
{
typedef null_type type;
};
template
class Tuple;
template
class Tuple: public map_tuple_to_cons::type
{
public:
typedef T0 E0T;
typedef T1 E1T;
typedef T2 E2T;
Tuple(const E0T& e0, const E1T& e1)
{ }
};
}//endof testNS
using namespace testNS;
template<typename T>
class TA{public:
template
void testfun(T1 e1, T2 e2)
{
Tuple t1(e1, e2);
//T1 _e1 = t1.template get<0>(); //works
* T1 _e1 = t1.get<0>(); //encounter compilor error
/*
the gcc3.4.2 error message:
syntax error before `;' token
the gcc4.2.4 error message:
error: expected primary-expression before ')' token
error: invalid operands of types '<unresolved overloaded function type>' and
'int' to binary 'operator<'
*/*
}
};
int main()
{
int i=10;
float f=10.5f;
TA<char> obj;
obj.testfun(i, f);
return 0;
}
By the way, how can i get contact with boost::tuple author.?
--
miss you!