std::tuple::get return const Type&&
std::tuple::get returning const Type&&
我刚刚阅读了 C++17 的更新界面,用于 cpp首选项 http://en.cppreference.com/w/cpp/utility/tuple/get std::tuple::get
,新的重载从get<>()
函数返回const Type&&
。 为什么要返回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 );
谷歌搜索出现了问题 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&&
的可变数据中移动。 它相对晦涩难懂。
相关文章:
- C++ 一个lambda浅拷贝const Type&如果它被赋予一个命名捕获,如[copy=val](){}?
- 如何修复" State Error (active) E0513 a value of type "const wchar_t *" cannot be assigned to an entity o
- 索引 2D 向量时'Reference to type const..'编译错误
- 为什么指针没有隐式转换为 <type> const*const
- const TYPE&- 我什么时候想通过引用 const 来使用?
- std::tuple::get return const Type&&
- std::remove_if 将“const type”作为“this”参数传递会丢弃 Linux 上的限定符
- `type'和`const type'的类型特征
- 当您声明"pointer type"形式(函数)参数"like an const array"时,什么是常量?
- 如何修复C++编译器错误"cannot convert 'Type' to 'const Type*'"?
- c++ C2440 错误无法从"const BWAPI::UpgradeType"转换为"const BWAPI::Type *"
- C++ 错误 C2662 无法将"this"指针从"const Type"转换为"Type &"
- 错误:调用"CBNET::CBNET(boost::reference_wrapper<const CBNET>::type&)"没有匹配函数
- 我很困惑为什么我的 fillDeck 函数说,"assigning to 'char *' from incompatible type 'const char *"
- "invalid initialization of non-const reference of type..." 克隆() 函数错误
- 没有匹配的操作数[template]/无法从Type const*转换为Type
- "invalid cast from type 'const myClass' to type 'int'",如何使其有效?
- 无法理解此错误... "Type 'const intSet' does not provide a subscript operator"这是与所有星号一致的
- 使用 Qt:使用 QList<Type *const> 时从 const void* 到 void* 的转换无效
- 为什么"TYPE* const"指针的行为不同?