std::tie允许隐式转换吗?

Are implicit conversions allowed with std::tie?

本文关键字:转换 tie std 许隐式      更新时间:2023-10-16

在c++11中,是否允许使用std::tie进行隐式转换?

下面的代码编译并运行,但我不确定幕后到底发生了什么,或者这是否安全。

std::tuple<float,float> foo() { return std::make_tuple(0,0); }
double a, b;
std::tie(a,b) = foo(); // a and b are doubles but foo() returns floats

使用元组的移动赋值操作符的模板版本

template< class... UTypes >
tuple& operator=(tuple<UTypes...>&& other );

使用自己的move-assignment语义逐个为单个元组成员进行move-assign。如果对应的成员是隐式可转换的,则隐式转换。

这基本上是std::pair中类似功能的自然扩展,我们已经享受了很长时间了。