我想从函数返回一些值,并且想将其打包在一个元组中。所以我有两种函数声明的可能性:
std::tuple<bool, string, int> f()
{
...
return std::make_tuple(false, "home", 0);
}
and
std::tuple<bool, string, int> f()
{
...
return std::forward_as_tuple(false, "home", 0);
}
这些函数是等价的吗?
在这些功能中,您更喜欢哪一个?
std::forward_as_tuple() http://en.cppreference.com/w/cpp/utility/tuple/forward_as_tuple创建一个元组参考。既然你要返回一个tuple<bool, string, int>
无论如何,两者最终是等价的在这种情况下,但我认为第一种方法更清晰 - 使用forward_as_tuple()
当你不转发任何东西时会很混乱。
另外,正如 Sebastian Redl 在评论中提到的,make_tuple()
将允许编译器执行复制省略 - 根据 C++11 标准第 12.8/31 段,而forward_tuple()
不会(因为它返回的类型与函数的返回类型不同)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)