我无法理解为什么以下内容(至少根据 gcc 4.8)在 C++11 中是合法的:
// This class manages a non-copyable resource.
struct B {
B();
B(B &&) { /* logging, etc., to verify if needed. */ }
private:
B(B const &);
B const &operator=(B const &);
};
B bar() {
B b;
// This is apparently allowed via the move constructor.
return b;
};
int main() {
// From this "side" of the call as well.
B b1 = bar();
B b2{bar()};
}
在什么情况下语言允许或实际首选移动构造函数?临时返回值似乎可以移动(和垃圾内容),但我想为所有可以静默使用移动的地方找到核心语言规则。谢谢!