Hello!
How can I iterate over a tuple (using C++0x)? I tried the following, but that doesn't work: -
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.
6 7969
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.
Is it impossible or what?
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.
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: -
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
Banfa 9,065
Expert Mod 8TB
I kind of thought there would be some clever template way of achieving this, glad you found it.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
by: wswilson |
last post by:
Here is my code:
listing = {'id': , 'name': }
I need to output:
id name
a Joe
b Jane
c Bob
|
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)'...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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,...
|
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...
| |