tags:

views:

60

answers:

1

I was going over C++0x. As i looked at tuple I saw this example. Why do I need to do get<3>(var)? Why can't I do var.get(index) or var.get<index>()? I prefer these to make code look and feel consistant.

typedef tuple< int, double, long &, const char * > test_tuple ;
long lengthy = 12 ;
test_tuple proof( 18, 6.5, lengthy, "Ciao!" ) ;
lengthy = get<0>(proof) ;  // Assign to 'lengthy' the value 18.
get<3>(proof) = " Beautiful!" ;  // Modify the tuple’s fourth element.
+6  A: 

You have to use get<0> because the tuple has a different type for each of its members. Therefore result type of get<0> is int, get<1> is double, get<2> is long& etc. You cannot achieve this when calling get(0) as it has to have a fixed return type.

You might also want to have a look at template metaprogramming because this pattern is one of the basic ones in this whole part of programming.

http://en.wikipedia.org/wiki/Template_metaprogramming
http://www.boost.org/doc/libs/1_43_0/libs/mpl/doc/index.html

dark_charlie
oh yeah... I completely forgot. I knew my C++ was rusty. Thanks for the major reminder.
acidzombie24
And of course, the reason you *can* write `var[3]` in Python is that it's a dynamically-typed language.
dan04