Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does std::get not have a single signature that accepts a forwarding reference

Why does std::get for std::tuple have so many overloads (http://en.cppreference.com/w/cpp/utility/tuple/get)? One corresponding to every possible combination of const & and &&? With each combination the const ref qualifiers on the return value is the same. Why not have just one single overload that accepts the tuple type by forwarding reference and then simply forward the return value based on the signature of the input? Something like so

template <int Index, typename TupleType>
decltype(auto) get(TupleType&& tup);

This sort of thing would make it really easy for people to reason about what the get function does and would avoid bugs like Issue 2485 (https://wg21.cmeerw.net/lwg/issue2485)

like image 907
Curious Avatar asked Mar 09 '23 10:03

Curious


1 Answers

std::get existed prior to decltype(auto). So that was easy.

Ok, why not change it?

The body of std::get is not specified by the standard, and should not be as different compilers have different layouts and implementations of tuples.

decltype(auto) does not tell the reader of the standard, the user or implementor of a compiler, what the return type is. It just says it is deduced from the body. Which the standard does not specify.

It being in the standard like that would thus be useless, unless they described what the return value was separately, which in the end would look a lot like listing overloads.

like image 109
Yakk - Adam Nevraumont Avatar answered Apr 06 '23 09:04

Yakk - Adam Nevraumont