4

Here is my code (of which I'm not sure it's right even):

template<typename... list> struct typeList;

template<typename, typename> struct zipper;
template<typename...L, typename...R> struct zipper<typeList<L...>, typeList<R...>>
{
    typedef std::tuple<std::pair<L,R>...> tuplez;
    static_assert(sizeof...(L)==sizeof...(R), "Mismatch number of Args...");

    static void print()
    {
    std::cout<<"The types are: "<<std::endl;
    for(int i=0; i<std::tuple_size<tuplez>::value ; ++i)
    std::cout<< "first : "<< typeid(typename std::tuple_element<i, tuplez>::type::first_type).name()<<
        "second : "<<typeid(typename std::tuple_element<i, tuplez>::type::second_type).name() <<std::endl;
    }
};

First of all I'm amazed how come my implementation of zipper is so small (must be something wrong I'm damn sure) . If my implementation is wrong, please lemme know why it's wrong rather than giving me solution.

Also If I run this code (zipper<typeList<int,double>, typeList<char,int>>::print();) , I get tons of error : the value of 'i' is not usable in a constant expression (from my gcc 4.7.1)

Here is full list of error (from ideone) with complete code: http://ideone.com/67nxM

P.S : Kindly edit the suitable question title, I couldn't comeup with any more reasonable.

Edit : Thanks to @KerrekSB and @Ugo for suggestions, here is the solution :

template<typename... list> struct typeList;

template<int n, typename T> struct printer
{
    static void print (){
        std::cout<< "first : "<< typeid(typename std::tuple_element<n, T>::type::first_type).name()<<
        "second : "<<typeid(typename std::tuple_element<n, T>::type::second_type).name() <<std::endl;
        printer<n-1,T>::print();
    }
};

template<typename, typename> struct zipper;
template<typename...L, typename...R> struct zipper<typeList<L...>, typeList<R...>>
{
    typedef std::tuple<std::pair<L,R>...> tuplez;

    static void print(){
    printer<std::tuple_size<tuplez>::value-1, tuplez>::print();
    }
};

template<typename T> struct printer<0,T>
{
    static void print (){
        std::cout<< "first : "<< typeid(typename std::tuple_element<0, T>::type::first_type).name()<<
        "second : "<<typeid(typename std::tuple_element<0, T>::type::second_type).name() <<std::endl;
    }
};
Community
  • 1
  • 1
Mr.Anubis
  • 5,132
  • 6
  • 29
  • 44
  • 2
    Related: http://stackoverflow.com/questions/11322095/how-to-make-a-function-that-zips-two-tuples-in-c11-stl – Flexo Aug 04 '12 at 09:39
  • 1
    Problem: `std::tuple_element` but `i` is a runtime value - you can't use it in that context. – Flexo Aug 04 '12 at 09:41
  • 1
    1) The implementation, bar `print()`, is fine. No need for the static assertion, as this just wouldn't compile with mismatched packs anyway. 2) Non-type template arguments must be compile-time constants. This is just basic template 101 and has nothing to do with the zipping. In TMP you should never have a `for` loop over `sizeof...(Args)`, but use static recursion. 3) No need to inherit from `tuple`, is there? – Kerrek SB Aug 04 '12 at 09:43

1 Answers1

1

Template non-type arguments must be constant expressions.
i here is a runtime variable, you cannot use it to as a template argument.

log0
  • 10,489
  • 4
  • 28
  • 62