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.
You have to use
get<0>because the tuple has a different type for each of its members. Therefore result type ofget<0>isint,get<1>isdouble,get<2>islong&etc. You cannot achieve this when callingget(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