Boost logo

Boost Users :

Subject: [Boost-users] How boost::tupe avoid compilor error of get member function?
From: simon Ye (kuangye19840410_at_[hidden])
Date: 2009-05-07 23:27:57


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<typename T1, typename T2>
    void testfun(T1 e1, T2 e2)
    {
        tuple<T1, T2> 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<int N, class T>
struct element
{
private:
  typedef typename T::tail_type Next;
public:
  typedef typename element<N-1, Next>::type type;
};

template<class T>
struct element<0,T>
{
  typedef typename T::head_type type;
};

struct null_type{};

template<typename HT, typename TT>
struct cons
{
  typedef HT head_type;
  typedef TT tail_type;

  template <int N>
  typename element<N, cons<HT, TT> >::type
  *get()* {
    typedef typename element<N, cons<HT, TT> >::type RetType;
    return RetType();
  }
};

template<typename HT>
struct cons<HT, null_type>
{
  typedef HT head_type;
  typedef null_type tail_type;
  typedef cons<HT, null_type> self_type;
  //get(BOOST_EXPLICIT_TEMPLATE_NON_TYPE(int, N))
  template <int N>
  typename element<N, self_type>::type
  *get()* {
    typedef typename element<N, cons<HT,null_type> >::type RetType;
    return RetType();
    //return boost::tuples::get<N>(*this);
  }
};

template <class T0, class T1, class T2>
struct map_tuple_to_cons
{
  typedef cons<T0,
               typename map_tuple_to_cons<T1, T2, null_type>::type
> type;
};

// The empty tuple is a null_type
template <>
struct map_tuple_to_cons<null_type, null_type, null_type>
{
  typedef null_type type;
};

template<typename T0, typename T1, typename T2=null_type>
class Tuple;

template<typename T0, typename T1, typename T2>
class Tuple: public map_tuple_to_cons<T0, T1, T2>::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<typename T1, typename T2>
    void testfun(T1 e1, T2 e2)
    {
        Tuple<T1, T2> 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!


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net