Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 );
like image 700
Curious Avatar asked Aug 12 '26 15:08

Curious


2 Answers

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.

like image 84
Dave S Avatar answered Aug 14 '26 05:08

Dave S


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

like image 38
Yakk - Adam Nevraumont Avatar answered Aug 14 '26 06:08

Yakk - Adam Nevraumont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!