开发者

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

开发者 https://www.devze.com 2023-03-25 09:28 出处:网络
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

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?

开发者_Python百科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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消