Gon*_*n I 6 c++ templates move-semantics
我正在努力理解移动和右值转换之间显示的不同行为
给出:
template <class T> void f1(T param) { }
struct B { B() = default; B(B&&) { cout << "Move constructor\n"; } };
void demo()
{
f1(static_cast<B&&>(B())); // does not call move constructor - constructor is elided
f1(move(B())); // calls move constructor - no elision
}
Run Code Online (Sandbox Code Playgroud)
移动调用强制调用移动构造函数,而静态转换则不会,并且构造函数显然被省略了。这看起来很奇怪,因为 std::move 的定义看起来很像静态转换。
为什么会有不同的行为?
更新:不能用 gcc 复制。仅使用 MSVC 显示的行为