的std ::元组的get()成员函数 [英] std::tuple get() member function

查看:1222
本文介绍了的std ::元组的get()成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的boost ::元组有使用这样的的get()成员函数:

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

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

看来的C ++ 0x 的std ::元组没有这个成员函数,你必须改用非成员函数形式:

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.

有什么特别的原因的std ::元组没有成员函数?或者只是我的实现(GCC 4.4)?

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

推荐答案

从的C ++ 0x草案:

From C++0x draft:

[注:原因得到的是一个非成员函数是,如果这个功能已经作为一个成员函数,code,其中的类型依赖于一个模板参数提供了将使用模板关键字所需。 - 注完]

[ 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 ]

这可以用这个code来说明:

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);
};

这篇关于的std ::元组的get()成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆