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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:02:58+00:00 2026-06-12T10:02:58+00:00

The following code was compiled and run in Visual Studio 2012 Express for Windows

  • 0

The following code was compiled and run in Visual Studio 2012 Express for Windows Desktop, as a learning exercise.

#include <cstdio>

class X
{
public:
    X()  { printf("default constructed\n"); }
    ~X() { printf("destructed\n");}
    X(const X&) { printf("copy constructed\n"); }
    X(X&&) { printf("move constructed\n"); }
    X & operator= (const X &) { printf("copy assignment operator\n"); }
};

X A() {
    X x;
    return x;
}

int main() {
    {
        A();
    }
    std::getchar();
}

When compiled with compiler optimizations disabled (/Od), the resulting output indicates that the destructor is called twice. This is a problem given that only one object is constructed. Why is the destructor being called twice? Wouldn’t this be a problem if the class was managing it own resources?

default constructed
move constructed
destructed
destructed   <<< Unexpected call 

I tried a couple of experiments to try and explain the output, but ultimately these didn’t lead to any useful explanations.

Experiment 1: When the same code is compiled with optimizations enabled (/O1 or /O2), the resulting output is:

default constructed
destructed

which indicates that the Named Return Value Optimization has elided the call to the move constructor, and masked the underlying problem.

Experiment 2: Disabled the optimization and commented out the move constructor. The output generated was what I expected.

default constructed
copy constructed
destructed
destructed
  • 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-12T10:02:59+00:00Added an answer on June 12, 2026 at 10:02 am

    Although Michael’s and jspcal answers are accurate, they didn’t answer the heart of my question, which was why were there two destructor calls made. I was expecting just one.

    The answer is that function A() returns a temporary object. Always. This is how function return values work, and move-semantics has no bearing on this fact. I guess Michael and jspcal assumed that I had not missed such a basic fact. I equated the term “moved” with the concept of “swap”. When swapped, objects are not constructed and destructed. Hence I was expecting just one destructor call.

    Since the returned object must be constructed and destructed, the second destructor call was made (and a second constructor call).

    Now, the actual constructor selected to be executed depends on what is provided in the class definition. If a move-constructor is available, that will be called. Otherwise the copy constructor will be called.

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

Sidebar

Related Questions

I have the following code compiled by gcc: #include <iostream> using namespace std; class
Why whenever I compile and run the following code in Visual Studio 2008: double
I have following code snippet that i use to compile class at the run
I am using Visual Studio 2003 to compile and run following program. There are
Consider the following code: #include<iostream> using namespace std; class sample { int a; int
Please look at the following code listing: #include <iostream> using namespace std; class Base
if I compile (under G++) and run the following code it prints Foo::Foo(int). However
When I compile the following code, I only see the error during Run-time which
The following code when compiled give this error I cannot understand why this is
I compiled the following code as a shared library using g++ -shared ... :

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.