简体   繁体   中英

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 );

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM