By vsoftco


2014-07-17 00:26:27 8 Comments

I have this piece of code:

#include <iostream>
#include <vector>

using namespace std;

class Foo{
public:
    Foo() noexcept {cout << "ctor" << endl;}
    Foo(const Foo&) noexcept {cout << "copy ctor" << endl;}
    Foo(Foo&&) noexcept {cout << "move ctor" << endl;}

    Foo& operator=(Foo&&) noexcept {cout << "move assn" << endl; return *this;}
    Foo& operator=(const Foo&) noexcept {cout << "copy assn" << endl; return *this;}

    ~Foo() noexcept {cout << "dtor" << endl;}
};


int main()
{   
    Foo foo;

    vector<Foo> v;
    v.push_back(std::move(foo)); 

    // comment the above 2 lines and replace by
    // vector<Foo> v{std::move(foo)}; 
}

The output is what I expect (compiled with g++ -std=c++11 --no-elide-constructors, same output without the flag)

ctor
move ctor
dtor
dtor

Now instead of using push_back initialize directly the vector v as

vector<Foo> v{std::move(foo)};

I do not understand why I get the outputs:

1) (without --no-elide-constructors)

ctor
move ctor
copy ctor
dtor
dtor
dtor

2) (with --no-elide-constructors)

ctor
move ctor
move ctor
copy ctor
dtor
dtor
dtor
dtor

In the first case, why is the copy ctor invoked? And in the second case, when the compiler does not perform elision, I have absolutely no idea why the move ctor is invoked twice. Any ideas?

2 comments

@Praetorian 2014-07-17 00:41:23

vector<Foo> v{std::move(foo)};

Here you're calling the vector constructor that takes an std::initializer_list. An initializer list only allows const access to its elements, so the vector is going to have to copy each element from the initializer_list to its own storage. That's what causes the call to the copy constructor.

From ยง8.5.4/5 [dcl.init.list]

An object of type std::initializer_list<E> is constructed from an initializer list as if the implementation allocated a temporary array of N elements of type const E, where N is the number of elements in the initializer list.


As for the extra move constructor call with -fno-elide-constructors, this was discussed in another answer a couple of days ago. It seems as though g++ takes a very literal approach to the example implementation of an initializer_list shown in the standard in same section I've quoted above.

The same example, when compiled using clang, doesn't produce the extra move constructor call.

@vsoftco 2014-07-17 00:49:27

Ohhh, got it... The syntax {...} for uniform initialization is quite tricky, and I now realized that vector is not an aggregate. And yes, same on my clang, no extra move. Great answer, thanks!

@Brian 2014-07-17 00:52:41

I agree with T.C.: the fact that gcc produces two move constructor calls seems like a bug. But I suppose as long as this behaviour is only observable when you pass -fno-elide-constructors, there's no problem.

@M.M 2014-07-17 00:55:14

T.C.'s answer nails it, I agree that clang's behaviour seems correct. The following code should be the same: Foo arr[1] = { std::move(foo) }; vector<Foo> v(arr, arr + 1); . The initializer_list should only serve the purpose of holding arr and arr + 1, not moving/copying the elements.

@Praetorian 2014-07-17 00:58:35

@MattMcNabb I agree, even if gcc'c behavior is not incorrect, it is definitely strange, clang's seems more intuitive. Anyway, as Brian says, as long as it only happens with -fno-elide-constructors, it's unlikely to bother anyone.

@Mooing Duck 2014-07-17 00:31:09

The containers try very hard to make sure they remain usable should an exception occur. As part of this, they'll only use std::move internally if your class' move constructor is exception safe. If it is not, (or it can't tell), it will copy just to be safe.

The correct move operations are

Foo(Foo&&) noexcept {cout << "move ctor" << endl;}
Foo& operator=(Foo&&) noexcept {cout << "move assn" << endl;}

@vsoftco 2014-07-17 00:37:00

Thanks, I changed it, now all of the ctors (together with the added operator=) are marked as noexcept, however I still get exactly the same output (cases 1) and 2) above). I use g++4.9 btw.

@Mooing Duck 2014-07-17 00:42:09

@vsoftco: Praetorian figured it out, click accept on his answer.

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] What is the easiest way to initialize a std::vector with hardcoded elements?

6 Answered Questions

[SOLVED] What is std::move(), and when should it be used?

1 Answered Questions

[SOLVED] Is the default Move constructor defined as noexcept?

4 Answered Questions

1 Answered Questions

1 Answered Questions

1 Answered Questions

2 Answered Questions

[SOLVED] Can modern C++ get you performance for free?

1 Answered Questions

[SOLVED] copy elision visible side effect

Sponsored Content