lenguador lenguador - 1 year ago 97
C++ Question

Are implicit conversions allowed with std::tie?

In c++11, are implicit conversions allowed with 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

Answer Source

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download