是否允许std::tie?

时间:2021-09-13 00:34:42

In c++11, are implicit conversions allowed with std::tie?

在c++11中,是否允许使用std::tie?

The following code compiles and runs but I'm not sure exactly what's going on behind the scenes or if this is safe.

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

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

1 个解决方案

#1


14  

What happens is the template version of tuple's move-assignment operator is used

所发生的是使用tuple的动作分配操作符的模板版本

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

which move-assigns individual tuple members one by one using their own move-assignment semantics. If the corresponding members are implicitly convertible - they get implicitly converted.

用它们自己的移动分配语义,一个一个地分配单个元组成员。如果相应的成员是隐式可转换的——它们将被隐式转换。

This is basically a natural extension of similar functionality in std::pair, which we've been enjoying for a long while already.

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

#1


14  

What happens is the template version of tuple's move-assignment operator is used

所发生的是使用tuple的动作分配操作符的模板版本

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

which move-assigns individual tuple members one by one using their own move-assignment semantics. If the corresponding members are implicitly convertible - they get implicitly converted.

用它们自己的移动分配语义,一个一个地分配单个元组成员。如果相应的成员是隐式可转换的——它们将被隐式转换。

This is basically a natural extension of similar functionality in std::pair, which we've been enjoying for a long while already.

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