【发布时间】:2019-09-26 03:25:16
【问题描述】:
我试图理解移动构造函数和右值引用。 所以我在https://www.onlinegdb.com/online_c++_compiler 上尝试了这段代码。 但结果让我很困惑。
#include <iostream>
#include <type_traits>
class A {
public:
A() { std::cout << "Constructed" << std::endl; }
A(const A& )= delete;
A(A&&) { std::cout << "Move Constructed" << std::endl; }
};
int
main ()
{
A&& a = A();
A b = a; // error: use of deleted function ‘A::A(const A&)’
//A b = static_cast<decltype(a)>(a); // This works, WTF?
std::cout << std::is_rvalue_reference<decltype(a)>::value << std::endl; // pretty sure a is rvalue reference.
return 0;
}
【问题讨论】:
-
decltype(id-expression)是一个奇怪的野兽,它与其他语言的规则不同,这里它给你A&&
标签: c++ c++11 rvalue-reference