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 9081623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:21:32+00:00 2026-06-16T20:21:32+00:00

Possible Duplicate: What are copy elision and return value optimization? I am having difficulty

  • 0

Possible Duplicate:
What are copy elision and return value optimization?

I am having difficulty understanding why in the following piece of code the copy constructor is not called.

#include <iostream>

class Test
{
public:
  Test(int){std::cout << "Test()" << std::endl;}
  Test(const Test&){std::cout << "Test(const Test&)" << std::endl;}
};

int main()
{
  // Test test;
  Test test2(Test(3));

  return 0;
}

Can someone explain why only the constructor is called and no copy constructor ?
Thanks.

  • 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-16T20:21:33+00:00Added an answer on June 16, 2026 at 8:21 pm

    This is called as copy elision.
    The compilers are allowed to do this optimization. Though it is not guaranteed by the standard any commercial compiler will perform this optimization whenever it can.


    Standard Reference:

    C++03 12.8.15:

    […] This elision of copy operations is
    permitted in the following
    circumstances (which may be combined
    to eliminate multiple copies):

    […]

    • when a temporary class object that has
      not been bound to a reference (12.2)
      would be copied to a class object with
      the same cv-unqualified type, the copy
      operation can be omitted by
      constructing the temporary object
      directly into the target of the
      omitted copy

    You might use some compiler settings to disable this optimization, like in case of gcc, from the man page:

    -fno-elide-constructor
    

    The C++ standard allows an implementation to omit creating a temporary which is only used to initialize another object of the same type. Specifying this option disables that optimization, and
    forces G++ to call the copy constructor in all cases.

    However, using this makes your code non portable across different compilers.

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

Sidebar

Related Questions

Possible Duplicate: What are copy elision and return value optimization? I have the following
Possible Duplicate: What are copy elision and return value optimization? why isn’t the copy
Possible Duplicate: Why copy constructor is not called in this case? Considering the following
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: question about copy constructor if I have this snippet of the code
Possible Duplicate: Does lastObject in NSMutableArray return a copy of the object? I'm following
Possible Duplicate: Why copy constructor is not called in this case? What are copy
Possible Duplicate: Why copy constructor is not called in this case? Consider the sample
Possible Duplicate: Returning unique_ptr from functions 20.7.1.2 [unique.ptr.single] defines copy constructor like this :

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.