![](/img/trans.png)
[英]volatile and const volatile std::tuple and std::get
[英]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.