I have the following code:
struct Foo {
Foo() = default;
Foo(const Foo&) { std::cout << "Copy" << std::endl; }
Foo(Foo&&) noexcept { std::cout << "Move" << std::endl; }
};
struct Bar {
Bar(Foo foo) : foo(std::move(foo)) {}
Foo foo;
};
int main() {
Foo foo;
std::cout << "Pass l-value:" << std::endl;
Bar barL(foo);
std::cout << "Pass r-value:" << std::endl;
Bar barR({});
}
It prints the following:
Pass l-value:
Copy
Move
Pass r-value:
Move
How compiler understands that it may not copy r-value?