iterate over tuple

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nielsp
    New Member
    • Jul 2009
    • 6

    iterate over tuple

    Hello!

    How can I iterate over a tuple (using C++0x)? I tried the following, but that doesn't work:
    Code:
    for(int i=0; i<std::tuple_size<T...>::value; ++i) 
        std::get<i>(my_tuple).do_sth();
    Error 1: sorry, unimplemented: cannot expand ‘Listener ...’ into a fixed-length argument list.
    Error 2: i cannot appear in a constant expression.

    So, how do I correctly iterate over the elements of a tuple?

    Thanks!

    Niels.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2

    Comment

    • nielsp
      New Member
      • Jul 2009
      • 6

      #3
      The document does not say anything about iterating over tuples. By the way, I am using tuples of C++0x. Although those tuples are probably derived from the boost library, it might be not absolutly the same.

      Niels.

      Comment

      • nielsp
        New Member
        • Jul 2009
        • 6

        #4
        Is it impossible or what?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by nielsp
          Error 1: sorry, unimplemented: cannot expand ‘Listener ...’ into a fixed-length argument list.
          Error 2: i cannot appear in a constant expression.
          Assuming error 1 is associated with your first line of code and error 2 is associated with the second line of code (on a side note it would be helpful if you clearly identified what line of code each error is associated with when posting code and errors in the future) then

          Error 2: The problem is with this std::get<i> you are trying to pass i as a parameter to a template but templates are resolved at compile time. You can only pass constants (or types were appropriate) as template parameters.

          As to error 1 and code line 1 the <T ...> construct is not something I am familiar with and there appears to be no type or variable or symbol with the name Listener in your code.

          Comment

          • nielsp
            New Member
            • Jul 2009
            • 6

            #6
            Hi!

            I understood the mistakes I made. The Listener-related error has nothing to do with the problem, that the symbol does'nt exists, I just wanted to simplify the code in order to make it easer to read, so I renamed Listener to T, but the error exists although (cannot expand 'T' ... instead of cannot expand 'Listener') .
            I got some helpful answers at stackoverflow.c om, so here is the version that works:
            Code:
            template<class TupleType, size_t N>
            struct do_iterate 
            {
            	static void call(TupleType& t) 
            	{
            		std::get<N>(t).do_sth(); 
            		do_iterate<TupleType, N-1>::call(t); 
            	}
            }; 
            	
            template<class TupleType>
            struct do_iterate<TupleType, 0> 
            {
            	static void call(TupleType& t) 
            	{
            		std::get<0>(t).do_sth(); 
            	}
            }; 
            
            template<class TupleType>
            void iterate_tuple(TupleType& t)
            {
            	do_iterate<TupleType, std::tuple_size<TupleType>::value-1>::call(t);
            }
            By the way: This will execute the do_sth()-operations of the tuple elements in reverse order.

            Niels

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              I kind of thought there would be some clever template way of achieving this, glad you found it.

              Comment

              Working...