On Apr 16, 10:37 am, german diago <germandi...@[EMAIL PROTECTED]
> wrote:
> Hello. I would like to propose a small addition to the tuple class
> template in c++0x. I'm surprised I didn't
> see this in any proposal, since it's gonna give us more than a
> headache (I think) if it's not included.
> Since the tuple class template is a container, it would be very useful
> to be able to loop over
> each element in the tuple. This is not easy, since that requires
> metaprogramming.
>
> A good small addition would be to do something like this:
>
> struct do_something
> {
> template <class T>
> void operator()(const T & elem)
> {
> //...
> }
>
> };
I think in most cases it would be impractical to fill that method in.
> int main()
> {
> tuple<int, float, std::string> t(3, 2.38f, "hello");
>
> tuple_elem_each(t, do_something());
>
> }
Technically there is nothing wrong with this, and I think that it
would actually be pretty easy to write this function. It would just
take the type of t as a template argument, and then pass it to a
simple template metaprogram.
However, tuples aren't really meant to hold arbitrarily sized
collections that need to be iterated over. They are only collections
in the sense that a struct is a collection of it's elements. They are
meant to be a convenient way to pack and unpack a small number of
variables of possibly different types. You should think of them as a
kind of anonymous struct for packing parameters.
Also, since there's a limit to the level of template recursion in a
given compiler, large enough tuples aren't ****table.
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|