Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6318557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:39:05+00:00 2026-05-24T15:39:05+00:00

In the following code, I expect A ‘s constructor is called, followed by A

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:39:05+00:00Added an answer on May 24, 2026 at 3:39 pm

    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]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would expect the following code to output hello5 . Instead, it only outputs
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code is in the /Courses/Detail action: [AcceptVerbs(GET)] public ActionResult Detail(int id) {
The following code executes a simple insert command. If it is called 2,000 times
I expect following code to put my span to the top-left corner of the
I have the following code that I expect to run successfully to completion but
The following code shows what I expect in Firefox and Chrome: a small white
I would expect the following code snippet to complain about trying to assign something
I expect with the following code 'max-button' be visible but it hides behind the
The following code returns 14 as you'd expect: Block[{expr}, expr = 2 z; f[z_]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.