Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force the compiler to use explicit copy constructor?

Tags:

c++

I wrote a small test program with a sample class containing also self-defined constructor, destructor, copy constructor and assignment operator. I was surprised when I realized that the copy constructor was not called at all, even though I implemented functions with return values of my class and lines like Object o1; Object o2(o1);

innerclass.hpp:

#include <iostream>

class OuterClass
{
public:
OuterClass()
{
    std::cout << "OuterClass Constructor" << std::endl;
}
~OuterClass()
{
    std::cout << "OuterClass Destructor" << std::endl;
}
OuterClass(const OuterClass & rhs)
{
    std::cout << "OuterClass Copy" << std::endl;
}
OuterClass & operator=(const OuterClass & rhs)
{
    std::cout << "OuterClass Assignment" << std::endl;
}

class InnerClass
{
public:
    InnerClass() : m_int(0)
    {
        std::cout << "InnerClass Constructor" << std::endl;
    }
    InnerClass(const InnerClass & rhs) : m_int(rhs.m_int)
    {
        std::cout << "InnerClass Copy" << std::endl;
    }
    InnerClass & operator=(const InnerClass & rhs)
    {
        std::cout << "InnerClass Assignment" << std::endl;
        m_int = rhs.m_int;
        return *this;
    }
    ~InnerClass()
    {
        std::cout << "InnerClass Destructor" << std::endl;
    }
    void sayHello()
    {
        std::cout << "Hello!" << std::endl;
    }

private:
    int m_int;
};

InnerClass innerClass()
{
    InnerClass ic;
    std::cout << "innerClass() method" << std::endl;
    return ic;
}
};

innerclass.cpp:

#include "innerclass.hpp"

int main(void)
{
std::cout << std::endl << "1st try:" << std::endl;


OuterClass oc;
OuterClass oc2(oc);
oc.innerClass().sayHello();

std::cout << std::endl << "2nd try:" << std::endl;

OuterClass::InnerClass ic(oc.innerClass());
ic = oc.innerClass();
}

Output:

 1st try:
 OuterClass Constructor
 OuterClass Copy
 InnerClass Constructor
 innerClass() method
 Hello!
 InnerClass Destructor

 2nd try:
 InnerClass Constructor
 innerClass() method
 InnerClass Constructor
 innerClass() method
 InnerClass Assignment
 InnerClass Destructor
 InnerClass Destructor
 OuterClass Destructor
 OuterClass Destructor

After some research I read that there is no guarantee that the compiler will use the explicitely defined copy constructor. I do not understand this behavior. Why does the copy constructor even exist then, if we do not know that it is called? How does the compiler decide if it uses it?

Or, even better, is there a way to force the compiler to use the self-defined copy constructor?

like image 951
Chris Avatar asked Dec 05 '22 06:12

Chris


2 Answers

Just for completeness with the other answers, the standard allows the compiler to omit the copy constructor in certain situations (what other answers refer to as "Return Value Optimization" or "Named Return Value Optimization" - RVO/NRVO):

12.8 Copying class objects, paragraph 15 (C++98)

Whenever a temporary class object is copied using a copy constructor, and this object and the copy have the same cv-unqualified type, an implementation is permitted to treat the original and the copy as two different ways of referring to the same object and not perform a copy at all, even if the class copy constructor or destructor have side effects. For a function with a class return type, if the expression in the return statement is the name of a local object, and the cv-unqualified type of the local object is the same as the function return type, an implementation is permitted to omit creating the temporary object to hold the function return value, even if the class copy constructor or destructor has side effects. In these cases, the object is destroyed at the later of times when the original and the copy would have been destroyed without the optimization.

So in your innerClass() method, the copy constructor you might think would be called at the return is permitted to be optimized away:

InnerClass innerClass() {
    InnerClass ic;
    std::cout << "innerClass() method" << std::endl;
    return ic;    // this might not call copy ctor
}
like image 151
Michael Burr Avatar answered Dec 07 '22 19:12

Michael Burr


I agree with Neil, you should not write a class which depends on the copy constructor being called. Namely because the compiler can do things like "Named Return Value Optimization" (link) which completely avoids the copy constructor in many return value scenarios. In order to enforce calling the copy constructor you'd need to write a lot of code aimed at "tricking" the C++ compiler. Not a good idea.

In a particular scenario though if you want to enforce calling the copy constructor, you can do an explicit call.

Object SomeFunc() {
  Object o1 = ...
  return Object(o1);
}
like image 27
JaredPar Avatar answered Dec 07 '22 20:12

JaredPar