Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++0x tuples have no iterators, right?

Looking into the standard N3291 I do not find any reference for tuple to support begin() and end(). But when I look at my notes from years back I seem to have jotted down that I need to look into that later. And here we are.

I can not find any trace of tuple<...>.begin() or tuple<...>.end() in the current C++0x standard, is this correct? It is not possible to pass a tuple with its iterators to an algorithm, nor can one for-loop over it, right?

tuple<int,string,double> val;
for(auto a : val) cerr << val;     // very wrong!

which is of course nonsense, because what should auto be?

I need the confirmation that my notes contain an error, and that there is no way to get those iterators for tuple elements. Or maybe there was an abandoned path in the standards discussion?

Note: I am aware of that one can use TMP or Variadic Templates to implement a do-for-all-elements-of-a-tuple, but my question is really about iterators.

like image 828
towi Avatar asked Jul 31 '11 12:07

towi


3 Answers

Boost.Fusion has iterable tuples -- boost::fusion::vector<> -- along with many iteration, query, and transformation algorithms for tuples.

It also has code to adapt boost::tuple<> for use with those iterators and algorithms; you could take that code and modify it to work with std::tuple<> instead.

like image 176
ildjarn Avatar answered Sep 18 '22 03:09

ildjarn


Iterators for tuples would be as useful as iterators for class members. Tuples just aren't meant to have that kind of--iterable--content.

like image 36
mike3996 Avatar answered Sep 19 '22 03:09

mike3996


No, there are no iterators for tuples. Iterators are a run-time concept, while tuples are a compile-time construct. As you rightly remarked, there isn't even a way to make sense of a generic tuple iterator.

If you need a type-erasing runtime container, you could use a vector of boost::anys.

like image 39
Kerrek SB Avatar answered Sep 20 '22 03:09

Kerrek SB