2
考慮:傳遞rvalue引用並使用std :: move()返回?
struct Foo {
Foo() { std::cout << "default ctor" << std::endl; }
Foo (const Foo&) { std::cout << "copy ctor" << std::endl; }
Foo& operator= (const Foo&) { std::cout << "copy op" << std::endl; return *this; }
Foo (Foo&&) { std::cout << "move ctor" << std::endl; }
Foo& operator= (Foo&&) { std::cout << "move op" << std::endl; return *this; }
~Foo() { std::cout << "dtor" << std::endl; }
};
Foo process1 (Foo&& foo) {
return foo;
}
Foo process2 (Foo&& foo) {
return std::move (foo);
}
與用法:
Foo foo {};
foo = process1 (std::move (foo));
給出了結果:
default ctor
copy ctor
move op
dtor
dtor
與用法:
Foo foo {};
foo = process2 (std::move (foo));
給出了結果:
default ctor
move ctor
move op
dtor
dtor
哪一個是首選的(process1或process2)?
這是否意味着在第一個示例(process1)中,如果我通過右函數的參數傳遞對象,該函數將返回一個Object,如果我不使用std::move()
?
編譯器:GCC 5.2.1