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; } }; 
3
  • 2
    Related: stackoverflow.com/questions/11322095/… Commented Aug 4, 2012 at 9:39
  • 1
    Problem: std::tuple_element<i, tuplez> but i is a runtime value - you can't use it in that context. Commented Aug 4, 2012 at 9: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? Commented Aug 4, 2012 at 9:43

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.