views:

143

answers:

2

boost::tuple has a get() member function used like this:

tuple<int, string, string> t(5, "foo", "bar");
cout << t.get<1>();  // outputs "foo"

It seems the C++0x std::tuple does not have this member function, and you have to instead use the non-member function form:

std::get<1>(t);

which to me looks uglier.

Is there any particular reason why std::tuple doesn't have the member function? Or is it just my implementation (gcc 4.4)?

+2  A: 

N3090/3092, §20.4.2.6/8: "Note: The reason get is a nonmember function is that if this functionality had been provided as a member function, code where the type depended on a template parameter would have required using the template keyword. —end note"

Jerry Coffin
+5  A: 

From C++0x draft:

[ Note: The reason get is a nonmember function is that if this functionality had been provided as a member function, code where the type depended on a template parameter would have required using the template keyword. — end note ]

This can be illustrated with this code:

template <typename T>
struct test
{
  T value;
  template <int ignored>
  T&  member_get ()
  {  return value;  }
};

template <int ignored, typename T>
T&  free_get (test <T>& x)
{  return x.value;  }

template <typename T>
void
bar ()
{
  test <T>  x;
  x.template member_get <0> ();  // template is required here
  free_get <0> (x);
};
doublep
I see. Still, they could have provided both member and non-member functions like boost, if for no other reason that to be compatible with boost (since a lot of people are likely to have already been using boost tuples before switching to C++0x).
HighCommander4