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

The Archive Base Latest Questions

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

Possible Duplicate: What are copy elision and return value optimization? why isn’t the copy

  • 0

Possible Duplicate:
What are copy elision and return value optimization?
why isn’t the copy constructor called

Why in the following code both gcc and clang don’t call copy constructor of A class even one
time (there is only one object created as destructor is called only one time).

class A
{
public:
  explicit A()
  {
    std::cout << "A()" << std::endl;
  }
  A(const A& toCp)
  {
    std::cout << "A(const A&)" << std::endl;
  }
  ~A()
  {
    std::cout << "~A()" << std::endl;
  }
  A& operator=(const A& toCp)
  {
    std::cout << "A::operator=" << std::endl;
  }
};

A fun()
{
  A x;
  std::cout << "fun" << std::endl;
  return x;
}

int main()
{
  A u = fun();
  return 0;
}

Printout of this code is:

A()
fun
~A()

I thought it copy constructor should be called 2 times (one for returning value and one in line A u = fun(7);

I used gcc and clang with -O0 for this code.

Any ideas?

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

    The compiler uses copy elision to avoid a copy (or move) for the return value of function fun(). This is a standard and trivial optimization which will almost always be invoked (depending on the compiler and its optimisation settings). The compiler may do this even if the elided copy (or move) constructor would have had side effects (as in your case, where it writes to stdout).

    note Copy elision is not restricted to inline functions, but will be used even if the function definition lives in a different compilation unit.

    • 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? I am having difficulty
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: question about copy constructor if I have this snippet of the code
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: dereferencing a pointer when passing by reference Is the copy constructor called
Possible Duplicate: Copy Constructor is not invoked # include <iostream> using namespace std; class
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.