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

  • SEARCH
  • Home
  • 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 8711951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:00:36+00:00 2026-06-13T05:00:36+00:00

Possible Duplicate: Why copy constructor is not called in this case? Considering the following

  • 0

Possible Duplicate:
Why copy constructor is not called in this case?

Considering the following code snippet:

#include <iostream>

using namespace std;

class Test
{
        char name[16];
        public:
        Test ()
        {
                cout <<"Inside Constructor"<<endl;
        }

        Test (const Test & t)
        {
                cout <<"Inside Copy Constructor "<<endl;
        }

};

Test f()
{
        return Test();
}

int main ( int argc, char ** argv)
{
        Test t;
        Test t1 = f();

}

Test t1= f() -> it invokes f(), and which returns the Test object, and then as per my understanding the copy constructor should be invoked. But i get the following output:

Inside Constructor
Inside Constructor

what is wrong with my understanding?.

  • 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-06-13T05:00:36+00:00Added an answer on June 13, 2026 at 5:00 am

    Two copy elisions occur here.

    The first is a kind of return value optimization (instead of copying the result of the expression Test() to the temporary object that is the return value of f, the expression Test() is evaluated by constructing directly into the temporary object that is the return value of f).

    The second is the elision of the copy from the initializer expression of t1 to t1 itself (so instead of copying the temporary that is the return value of f into t1, the temporary that is the return value of f is constructed directly into t1).

    The two elisions chain together — so the memory of t1 is used as the destination when constructing the return value of f, and the memory for the return value of f is used as the destination when constructing Test(). So in effect t1 is direct-initialized by the no-args constructor and there are no copies needed.

    Copy constructor elision is defined in the standard in 12.8/15 of C++03 and 12.8/31 of C++11 (which also allows elision of moves). It requires specific permission because it changes the observable behavior of the program (in your case it omits the side-effect of the copy constructor, the output). So it can only be performed under the conditions defined in the standard.

    Both of these elisions are examples of the second permitted elision in C++03 (third in C++11), when the source is a temporary.

    The first permitted elision is often called the “named return value optimization”, and it allows a particular kind of copy to be omitted when the source is not a temporary.

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

Sidebar

Related Questions

Possible Duplicate: Why copy constructor is not called in this case? In the following
Possible Duplicate: Copy Constructor is not invoked # include <iostream> using namespace std; class
Possible Duplicate: Why copy constructor is not called in this case? Consider the sample
Possible Duplicate: question about copy constructor if I have this snippet of the code
Possible Duplicate: Returning unique_ptr from functions 20.7.1.2 [unique.ptr.single] defines copy constructor like this :
This is not a duplicate of Implementing the copy constructor in terms of operator=
Possible Duplicate: dereferencing a pointer when passing by reference Is the copy constructor called
Possible Duplicate: Why ‘this’ is a pointer and not a reference? Is there any
Possible Duplicate: Why has the destructor been called only once? Given the code below,
Possible Duplicate: What's the most reliable way to prohibit a copy constructor in C++?

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.