Curious
Curious

Reputation: 21510

std::tuple::get returning const Type&&

I just read the updated interface for C++17 for std::tuple::get on cppreference http://en.cppreference.com/w/cpp/utility/tuple/get and the new overloads return const Type&& from the get<>() functions. Why would you want to return a const Type&& over just a regular const Type&? You can't move from instances of either type..

For reference these are the function declarations I am referring to

template< class T, class... Types >
constexpr const T&& get(const tuple<Types...>&& t);

and

template< std::size_t I, class... Types >
constexpr std::tuple_element_t<I, tuple<Types...> >const&&
get( const tuple<Types...>&& t );

Upvotes: 3

Views: 936

Answers (2)

Dave S
Dave S

Reputation: 21113

A google search turned up Issue 2485, which pointed to a flaw like this:

#include <functional>
#include <string>
#include <tuple>

using namespace std; 

string str1() { return "one"; }
const string str2() { return "two"; }
tuple<string> tup3() { return make_tuple("three"); }
const tuple<string> tup4() { return make_tuple("four"); }

int main() {
  // cref(str1()); // BAD, properly rejected
  // cref(str2()); // BAD, properly rejected
  // cref(get<0>(tup3())); // BAD, properly rejected
  cref(get<0>(tup4())); // BAD, but improperly accepted!
}

In this particular case, cref has a deleted overload for const T&&, but passing it through get is obscuring that the tuple, and its member, is a temporary.

Upvotes: 4

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275405

You can move from mutable data from a const&&. It is relatively obscure.

Upvotes: 2

Related Questions