boost::tuple
有一个 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::tuple
没有这个成员函数,你必须改用非成员函数形式:
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);
在我看来哪个更丑.
std::tuple
没有成员函数有什么特别的原因吗?还是只是我的实现(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:
[ 注意:get 是非成员函数的原因是,如果此功能已作为成员函数提供,则类型依赖于模板参数的代码将需要使用模板关键字.— 尾注 ]
[ 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);
};
这篇关于std::tuple get() 成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!