473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

iterate over tuple

6
Hello!

How can I iterate over a tuple (using C++0x)? I tried the following, but that doesn't work:
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<std::tuple_size<T...>::value; ++i) 
  2.     std::get<i>(my_tuple).do_sth();
  3.  
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.
Jul 28 '09 #1
6 7969
nielsp
6
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.
Jul 29 '09 #3
nielsp
6
Is it impossible or what?
Jul 30 '09 #4
Banfa
9,065 Expert Mod 8TB
@nielsp
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.
Jul 30 '09 #5
nielsp
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.com, so here is the version that works:
Expand|Select|Wrap|Line Numbers
  1. template<class TupleType, size_t N>
  2. struct do_iterate 
  3. {
  4.     static void call(TupleType& t) 
  5.     {
  6.         std::get<N>(t).do_sth(); 
  7.         do_iterate<TupleType, N-1>::call(t); 
  8.     }
  9. }; 
  10.  
  11. template<class TupleType>
  12. struct do_iterate<TupleType, 0> 
  13. {
  14.     static void call(TupleType& t) 
  15.     {
  16.         std::get<0>(t).do_sth(); 
  17.     }
  18. }; 
  19.  
  20. template<class TupleType>
  21. void iterate_tuple(TupleType& t)
  22. {
  23.     do_iterate<TupleType, std::tuple_size<TupleType>::value-1>::call(t);
  24. }
  25.  
By the way: This will execute the do_sth()-operations of the tuple elements in reverse order.

Niels
Jul 31 '09 #6
Banfa
9,065 Expert Mod 8TB
I kind of thought there would be some clever template way of achieving this, glad you found it.
Jul 31 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Yomanium Yoth Taripoät II | last post by:
HI, 1) what are the differences between list and tuple? 2) how to concatenate tuple and list? no method, no opérator? 3) im looking the fucking manual, and cant add value in my tuple, when it...
50
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this,...
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
4
by: James Stroud | last post by:
Hello, Its not obvious to me how to do this. I would like to iterate using a tuple as an index. Say I have two equivalently sized arrays, what I do now seems inelegant: for index, list1_item...
2
by: Alan Isaac | last post by:
I am probably confused about immutable types. But for now my questions boil down to these two: - what does ``tuple.__init__`` do? - what is the signature of ``tuple.__init__``? These...
4
by: wswilson | last post by:
Here is my code: listing = {'id': , 'name': } I need to output: id name a Joe b Jane c Bob
13
by: stef mientki | last post by:
hello, I generate dynamically a sequence of values, but this "sequence" could also have length 1 or even length 0. So I get some line in the form of: line = '(2,3,4)' line = '' line = '(2)'...
4
by: Alex Vinokur | last post by:
Here is some tuple (triple in this case) with uniform types (for instance, double): boost::tuple<double, double, doublet; Is there any way (in boost::tuple) to define such tuples something like...
0
by: Hatem Nassrat | last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote: Well I have looked into this and it seems that using the list comprehension is faster, which is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.