我刚刚阅读了 std::tuple::get
的 C++17 更新接口(interface)关于 cppreference http://en.cppreference.com/w/cpp/utility/tuple/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 );
最佳答案
Google 搜索结果 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
传递它会掩盖元组,并且它的成员是临时的。
关于c++ - std::tuple::get 返回常量类型&&,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43131543/