繁体   English   中英

std :: tuple :: get返回const Type &&

[英]std::tuple::get returning const Type&&

我只是在cppreference http://en.cppreference.com/w/cpp/utility/tuple/get上阅读了std::tuple::get C ++ 17更新接口,新的重载从get<>()返回了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 );

谷歌搜索出现了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会掩盖元组及其成员是临时的。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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