std::tuple get() member function(std::tuple get() 成员函数)
问题描述
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() 成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::tuple get() 成员函数


基础教程推荐
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01