Jason Yu
Jason Yu

Reputation: 2038

std::make_tuple and rvalue reference

Like below, why "vv" here is not an rvalue-reference? Here the type of "v" is rvalue-reference, and according to the inference rule of decltype, the decltype of an rvalue-reference (not a prvalue) should be the type of rvalue-reference, right?

int main(int argc, char **argv) {
  int x{};
  const auto [v] = std::make_tuple<int&&>(std::move(x));  // v -> const int &&;
  decltype(v) vv = 10;  // vv -> const int;
  return 0;
}

Upvotes: 2

Views: 484

Answers (1)

C.M.
C.M.

Reputation: 3383

Happens because std::make_tuple<int&&> returns std::tuple<int>. From cppreference:

template<class... Types>
tuple<VTypes...> make_tuple( Types&&... args );

For each Ti in Types..., the corresponding type Vi in VTypes... is std::decay<Ti>::type unless application of std::decay results in std::reference_wrapper<X> for some type X, in which case the deduced type is X&.

If you use std::tuple instead of std::make_tuple -- it works as you expect it to.

Here is code to play.

Upvotes: 2

Related Questions