2

This might just be a "vocabulary issue", but there is something I don't get with rvalue references and pass by value.

If I have a code like this (ideone it)

struct Foo
{
    Foo()                 { printf("Ctor\n"); }
    Foo(const Foo& other) { printf("Copy\n"); }
    Foo(Foo&& other)      { printf("Move\n"); }
    ~Foo()                { printf("Dtor\n"); }
};

void DoFoo(Foo foo) { printf("Foo\n"); }

int main()
{
    Foo foo;
    DoFoo(foo);
    DoFoo(Foo{});
    return 0;
}

it will give me this output (ideone result copy pasted)

Ctor 0x96eaa10 0
Copy 0x96eaa20 0
Foo  0x96eaa20 0
Dtor 0x96eaa20
Ctor 0x96eaa20 1
Foo  0x96eaa20 1
Dtor 0x96eaa20
Dtor 0x96eaa10

What I don't understand is, according to many stackoverflow answers, like here or here, the move constructor should be called for the DoFoo(Foo{}), but it's clearly not, and the behaviour is the same with or without a Foo move constructor.

Even replacing DoFoo(Foo foo) by DoFoo(Foo&& foo) does not force the call to the move constructor.

So, is it just a vocabulary issue when using moved in these particular case ? Is it a GCC particular behaviour? or anything else ?

Community
  • 1
  • 1
cmourglia
  • 2,423
  • 1
  • 17
  • 33
  • Yes sure, I feel rather stupid not having thought about it actually... Was totally focusing on the `move constructed` fact and forgot about copy elision. You can make an answer (with a bit more explanations about copy elision) I'll accept it. – cmourglia Jul 14 '16 at 13:01
  • `DoFoo(Foo&&)` would certainly **not** call any move constructor, since it accepts a reference. (Since C++17, neither does `DoFoo(Foo)` with a prvalue argument.) – Davis Herring Nov 01 '19 at 03:51

0 Answers0