Search code examples
c++c++11g++icc

Why the move constructor doesn't get invoked in this case?


I was following this article Ten C++11 Features Every C++ Developer Should Use and added some basic tracing to the code of the Move semantics example and see that the move constructor is never invoked and wonder just why. I have tried with both compilers GNU 4.6.3 and Intel 15.0.0 and the result is the same.

I compile it like this:

# using Intel compiler
icpc -Wall -g -Wno-shadow -std=c++0x -o showcase ./showcase.cpp

# using gnu g++ compiler
g++ -Wall -g -Wno-shadow -std=gnu++0x -o showcase ./showcase.cpp

This is the output I get where the move constructor is not invoked when it should at line 133:

instantiating b1 ...
Buffer() default constructor invoked 
my name is: 
instantiating b2 ...
Buffer(const std::string& name, size_t size) constructor invoked 
my name is: buf2
instantiating b3 ...
Buffer(const Buffer& copy) copy constructor invoked 
my name is: buf2
instantiating b4 ...
Buffer(const std::string& name, size_t size) constructor invoked 
my name is: buf64
moving getBuffer<int>("buf5") to b1 ...
Buffer(const std::string& name, size_t size) constructor invoked 
Buffer& operator=(Buffer&& temp) move assignment operator invoked
my name is: buf5

Here is the code:

#include <assert.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>

#include <map>
#include <vector>
#include <memory>
#include <algorithm>

using namespace std;

//============================================================================
// Classes
//============================================================================

template <typename T>
class Buffer 
{
   std::string          _name;
   size_t               _size;
   std::unique_ptr<T[]> _buffer;

public:
   // default constructor
   Buffer():
      _size(16),
      _buffer(new T[16]) {
      cout << "Buffer() default constructor invoked " << endl;
   }

   // constructor
   Buffer(const std::string& name, size_t size):
      _name(name),
      _size(size),
      _buffer(new T[size]) {
      cout << "Buffer(const std::string& name, size_t size) constructor invoked " << endl;
   }

   // copy constructor
   Buffer(const Buffer& copy):
      _name(copy._name),
      _size(copy._size),
      _buffer(new T[copy._size])
   {
      cout << "Buffer(const Buffer& copy) copy constructor invoked " << endl;
      T* source = copy._buffer.get();
      T* dest = _buffer.get();
      std::copy(source, source + copy._size, dest);
   }

   void print_name() const {
        cout << "my name is: " << _name << endl;
   }

   // copy assignment operator
   Buffer& operator=(const Buffer& copy)
   {
      cout << "Buffer& operator=(const Buffer& copy) assignment operator invoked " << endl;
      if(this != &copy)
      {
         _name = copy._name;

         if(_size != copy._size)
         {
            _buffer = nullptr;
            _size = copy._size;
            _buffer = _size > 0 ? new T[_size] : nullptr;
         }

         T* source = copy._buffer.get();
         T* dest = _buffer.get();
         std::copy(source, source + copy._size, dest);
      }

      return *this;
   }

   // move constructor
   Buffer(Buffer&& temp):
      _name(std::move(temp._name)),
      _size(temp._size),
      _buffer(std::move(temp._buffer))
   {
      cout << "Buffer(Buffer&& temp) move constructor invoked" << endl;
      temp._buffer = nullptr;
      temp._size = 0;
   }

   // move assignment operator
   Buffer& operator=(Buffer&& temp)
   {
      cout << "Buffer& operator=(Buffer&& temp) move assignment operator invoked" << endl;
      assert(this != &temp); // assert if this is not a temporary

      _buffer = nullptr;
      _size = temp._size;
      _buffer = std::move(temp._buffer);

      _name = std::move(temp._name);

      temp._buffer = nullptr;
      temp._size = 0;

      return *this;
   }
};

template <typename T>
Buffer<T> getBuffer(const std::string& name) {
   Buffer<T> b(name, 128);
   return b;
}

//============================================================================
// Main
//============================================================================

int main(int argc, char** argv) {
    cout << "**************** move semantics" << endl;
    cout << "instantiating b1 ..." << endl;
    Buffer<int> b1;
    b1.print_name();
    cout << "instantiating b2 ..." << endl;
    Buffer<int> b2("buf2", 64);
    b2.print_name();
    cout << "instantiating b3 ..." << endl;
    Buffer<int> b3 = b2;
    b3.print_name();
    cout << "instantiating b4 by moving from a temp object ..." << endl;
    Buffer<int> b4 = getBuffer<int>("buf64"); // Buffer<int>("buf4", 64);
    b4.print_name();
    cout << "moving getBuffer<int>(\"buf5\") to b1 ..." << endl;
    b1 = getBuffer<int>("buf5");
    b1.print_name();

    return EXIT_SUCCESS;
}

Solution

  • The move assignment operator is correctly invoked.

    For the case where you expected a move construction, the b4, you're getting return value optimization (RVO) where the result object is directly constructed in caller-provided storage. Whether this happens depends on the compiler and options: it's permitted but not required. I.e. it's a Quality of Implementation issue.


    Note that it's not a good idea to use e.g. -fno-elide-constructors to avoid this. RVO is much more efficient than ordinary construction plus move construction. It has to be, since it's less.