Eric Z
Eric Z

Reputation: 14505

Compiler optimization of implicit constructor conversion

In the following code, I expect A's constructor is called, followed by A's copy constructor. However, It turns out only constructor is get called.

// MSVC++ 2008
class A
{
public:
   A(int i):m_i(i)
   {
      cout << "constructor\n";
   }
   A(const A& a)
   {
      m_i = a.m_i;
      cout << "copy constructor\n";
   }

private:
   int m_i;
};

int main()
{
   // only A::A() is called
   A a = 1;
   return 0;
}

I guess the compiler is smart enough to optimize away the second call, to initialize the object a directly with the constructor. So is it a standard-defined behavior or just implementation-defined?

Upvotes: 2

Views: 430

Answers (2)

In silico
In silico

Reputation: 52129

It's standard, but there's no optimization involved.

Actually, I believe there is an optimization involved, but it's still entirely standard.

This code:

A a = 1;

invokes the converting constructor†† of A. A has a single converting constructor A(int i) that allows an implicit conversion from int to A.

If you prepend the constructor declaration with explicit, you'll find the code won't compile.

class A
{
public:
    explicit A(int i) : m_i(i) // Note "explicit"
    {
        cout << "constructor\n";
    }

    A(const A& a)
    {
        m_i = a.m_i;
        cout << "copy constructor\n";
    }

private:
    int m_i;
};

void TakeA(A a)
{
}

int main()
{
    A a = 1;     // Doesn't compile
    A a(1);      // Does compile
    TakeA(1);    // Doesn't compile
    TakeA(A(1)); // Does compile
    return 0;
}

† After looking at the standard again, I may have been initially wrong.

8.5 Initializers [dcl.init]

12. The initialization that occurs in argument passing, function return, throwing an exception (15.1), handling an exception (15.3), and brace-enclosed initializer lists (8.5.1) is called copy-initialization and is equivalent to the form

   T x = a;

14. The semantics of initializers are as follows. The destination type is the type of the object or reference being initialized and the source type is the type of the initializer expression. The source type is not defined when the initializer is brace-enclosed or when it is a parenthesized list of expressions.

...

  • If the destination type is a (possibly cv-qualified) class type:
    • If the class is an aggregate (8.5.1), and the initializer is a brace-enclosed list, see 8.5.1.
    • If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, constructors are considered. The applicable constructors are enumerated (13.3.1.3), and the best one is chosen through overload resolution (13.3). The constructor so selected is called to initialize the object, with the initializer expression(s) as its argument(s). If no constructor applies, or the overload resolution is ambiguous, the initialization is ill-formed.
    • Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one is chosen through overload resolution (13.3). If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the destination type. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized; see 12.2, 12.8.

...

So in one sense it is very much an optimization. But I wouldn't worry about it since it is explicitly allowed by the standard and just about every compiler nowadays does the elison.

For a much more thorough treatment on initialization, see this GotW article (#36). The article seems to agree with the above interpretation of the standard:

NOTE: In the last case ("T t3 = u;") the compiler could call both the user-defined conversion (to create a temporary object) and the T copy constructor (to construct t3 from the temporary), or it could choose to elide the temporary and construct t3 directly from u (which would end up being equivalent to "T t3(u);"). Since July 1997 and in the final draft standard, the compiler's latitude to elide temporary objects has been restricted, but it is still allowed for this optimization and for the return value optimization.

††

ISO/IEC 14882:2003 C++ Standard reference

12.3.1 Conversion by constructor [class.conv.ctor]

1. A constructor declared without the function-specifier explicit that can be called with a single parameter specifies a conversion from the type of its first parameter to the type of its class. Such a constructor is called a converting constructor. [Example:

     class X {
         // ...
     public:
         X(int);
         X(const char*, int =0);
     };

     void f(X arg)
     {
         X a = 1;        // a = X(1)
         X b = "Jessie"; // b = X("Jessie",0)
         a = 2;          // a = X(2)
         f(3);           // f(X(3))
     }

—end example]

2. An explicit constructor constructs objects just like non-explicit constructors, but does so only where the direct-initialization syntax (8.5) or where casts (5.2.9, 5.4) are explicitly used. A default constructor may be an explicit constructor; such a constructor will be used to perform default-initialization or valueinitialization (8.5). [Example:

     class Z {
     public:
         explicit Z();
         explicit Z(int);
         // ...
     };

     Z a;                      // OK: default-initialization performed
     Z a1 = 1;                 // error: no implicit conversion
     Z a3 = Z(1);              // OK: direct initialization syntax used
     Z a2(1);                  // OK: direct initialization syntax used
     Z* p = new Z(1);          // OK: direct initialization syntax used
     Z a4 = (Z)1;              // OK: explicit cast used
     Z a5 = static_cast<Z>(1); // OK: explicit cast used

—end example]

Upvotes: 6

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

No optimization here. When = is used in the initialization, it is eqivalent(nearly) to calling the constructor with the right hand side as an argument. So this:

A a = 1;

Is (mostly) equivalent to this:

A a(1);

Upvotes: 2

Related Questions