I would expect the following code to print copy called twice. Once for the creation of f2 and once for the creation of f3. But for some reason, it is only printed once. Why is this? Is there some type of optimization going on? If so, is there a way to disable it without compiler flags?
struct Foo
{
Foo() = default;
Foo(Foo const&) { std::cout << "copy called" << std::endl; }
};
Foo test()
{
Foo f1;
Foo f2(f1);
return f2;
}
int main()
{
Foo f3(test());
}