473,792 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

iterate over tuple

6 New Member
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 8002
weaknessforcats
9,208 Recognized Expert Moderator Expert
http://www.boost.org/doc/libs/1_39_0...ers_guide.html
Jul 29 '09 #2
nielsp
6 New Member
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 New Member
Is it impossible or what?
Jul 30 '09 #4
Banfa
9,065 Recognized Expert Moderator Expert
@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 New Member
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:
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 Recognized Expert Moderator Expert
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
4082
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 already created :/ how to do it? thx.
50
3693
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, I'm just curious why it is neccesary.. Thanks,
1
3261
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 pickle it? Thanks , Fedor
4
1638
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 in enumerate(firstlist): do_something(list1_item, secondlist) I would like something more like this:
2
3487
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 questions are stimulated by http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303439 Looking at that, what fails if I leave out the following line? ::
4
2055
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
2413
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)' (in fact these are not constant numbers, but all kind of integer
4
2435
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 uniform_tuple <3, doublet; ? Alex Vinokur
0
1794
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 reasonable since generators require iteration and stop iteration and what not.
0
9670
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10430
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10159
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9033
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.