考虑:
std::tuple<int , const A&> func (const A& a)
{
return std::make_tuple( 0 , std::ref(a) );
}
编写正确且可移植的代码是否需要 std::ref
? (没有它编译很好)
背景:
如果我删除 std::ref
我的代码构建良好,没有任何警告( g++-4.6 -Wall
),但运行不正确。
如果感兴趣,定义 A
:
struct A {
std::array<int,2> vec;
typedef int type_t;
template<typename... OPs,typename... VALs>
A& operator=(const std::pair< std::tuple<VALs...> , std::tuple<OPs...> >& e) {
for( int i = 0 ; i < vec.size() ; ++i ) {
vec[i] = eval( extract(i,e.first) , e.second );
}
}
};
原文由 ritter 发布,翻译遵循 CC BY-SA 4.0 许可协议
make_tuple(0, a)
制作一个tuple<int, A>
。make_tuple(0, ref(a))
制作一个tuple<int, reference_wrapper<A>>
。tuple<int, A&> t(0, a);
对于你不能用make_tuple
制作的元组,或者使用std::tie
。