我不得不遵循下面的一段代碼,我想知道標準對它的說明。它是未定義的行爲,已定義但未指定的行爲或明確定義的行爲?Rvalue綁定和移動的組合
using namespace std;
struct Foo {
mutable int obj;
Foo(Foo&&) = default;
Foo(int arg) : obj(arg) {}
void f() const { obj *= 2; }
};
int main()
{
Foo&& a = Foo(5); // Binds temporary, lifetime ends with program
const Foo& b = a; // Binds a, lifetime ends with program
Foo c(std::move(a)); // Moves from a
// a now in defined, but unspecified state
b.f(); // ??
cout << b.obj << endl; // May output 10
return 0;
} // End of lifetime of temporary
註解和我對標準的理解和解釋是否正確?
爲什麼地球上會有這種未定義的行爲?標量的移動狀態與移動前的狀態完全相同。這是一個副本。 – Columbo 2015-02-11 15:24:22
「有效但未指定的狀態」是一個約定,後面是大多數標準庫類,關於它們的移動操作如何影響移出的對象。關於編譯器生成的移動構造函數沒有任何說明,或者從'Foo'移動的狀態。 – 2015-02-11 15:37:16
@ T.C。啊,這就是我誤解了標準的地方......好吧,它不太容易閱讀 – WorldSEnder 2015-02-11 15:48:43