std :: tuple :: get returns const Type&&

时间:2017-03-31 02:47:51

标签: c++ tuples c++14 rvalue-reference c++17

我刚刚在cppreference http://en.cppreference.com/w/cpp/utility/tuple/get上阅读了std::tuple::get的C ++ 17更新界面,新的重载从const Type&&函数返回get<>()。您为什么要仅定期const Type&&返回const Type&?你不能从任何一种类型的实例移动..

作为参考,这些是我所指的函数声明

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

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

2 个答案:

答案 0 :(得分:3)

谷歌搜索出现Issue 2485,这表明存在这样的缺陷:

#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!
}

在这种特殊情况下,cref已删除const T&&的重载,但将其传递给get会模糊元组及其成员是临时的。

答案 1 :(得分:1)

您可以从const&&的可变数据中移除。这是相对模糊的。