8

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
lenguador
  • 476
  • 5
  • 9

1 Answers1

15

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.

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

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • perhaps it's a good idea to mention that the implicit conversion take place inside the body of `operator=`, because during argument deduction of the `UTypes...` implicit conversions are ignored. – TemplateRex Oct 26 '16 at 19:01