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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:23:59+00:00 2026-06-03T11:23:59+00:00

#include <iostream> #include <string> #include <vector> using namespace std; struct Exmpl{ Exmpl() { cout

  • 0
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Exmpl{
    Exmpl()
    {
        cout << "Exmpl()" << endl;
    }

    Exmpl(const Exmpl&)
    {
        cout << "Exmpl(const Exmpl&)" << endl;
    }

    Exmpl& operator=(const Exmpl& rhs)
    {
        cout << "operator=Exmpl()" << endl;
        return *this;
    }

    ~Exmpl()
   {
        cout << "~Exmpl()" << endl;
   }
};

void func1(Exmpl obj)
{
}

void func2(Exmpl &obj)
{
}

Exmpl func3()
{
    Exmpl obj;
    return obj;
}

int main()
{
    Exmpl eobj;
    func1(eobj);
    func2(eobj);
    eobj = func3();
    Exmpl *p = new Exmpl;
    vector<Exmpl> evec(3);

    delete p;
    return 0;
}

when compiled in g++(4.4.3) I got

Exmpl()
Exmpl(const Exmpl&)
~Exmpl()
Exmpl()
operator=(const Exmpl&)
~Exmpl()
Exmpl()
Exmpl()
Exmpl(const Exmpl&)
Exmpl(const Exmpl&)
Exmpl(const Exmpl&)
~Exmpl()
~Exmpl()
~Exmpl()
~Exmpl()
~Exmpl()
~Exmpl()

and in vs2008, the result is:

Exmpl()
Exmpl(const Exmpl&)
~Exmpl()
Exmpl()
Exmpl(const Exmpl&)
~Exmpl() 
operator=(const Exmpl&)
~Exmpl()
Exmpl()
Exmpl()
Exmpl(const Exmpl&)
Exmpl(const Exmpl&)
Exmpl(const Exmpl&)
~Exmpl()
~Exmpl()
~Exmpl()
~Exmpl()
~Exmpl()
~Exmpl()

when the code goes to “eobj = func3();” in main, the 5th and 6th lines in vs2008 result can’t be found in g++. I’ve tried to several levels of optimize, but the result is the same. what’s the reason of the difference?

  • 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-03T11:24:05+00:00Added an answer on June 3, 2026 at 11:24 am

    C++ allows copy constructors to be elided when an object is being returned as a value from a function (as in func3()). Even if the constructor has a side effect other than constructing the new object – in this case the copy constructor writes to cout.

    g++ is doing that even with no optimization specified, while MSVC will do that only if you ask for optimization to be performed.

    If you reduce the program to the following (just to cut the output down to the interesting parts):

    int main()
    {
        Exmpl eobj;
        eobj = func3();
    
        return 0;
    }
    

    You’ll see the following for programs generated with these command lines (tests done with MinGW 4.6.1 and MSVC 16.0, otherwise known as VC++ 2010):

    • g++ -O0 -o test.exe test.cpp (g++, ‘no’ optimizations)
    • g++ -O2 -o test.exe test.cpp (g++, optimizations)
    • cl /Ox /EHsc test.cpp (msvc, optimziations)

      Exmpl()
      Exmpl()
      operator=Exmpl()
      ~Exmpl()
      ~Exmpl()
      
    • cl /EHsc test.cpp (msvc, no optimizations)

    • g++ -fno-elide-constructors -o test.exe test.cpp (g++ with no constructors elided as suggested by Jesse Good)

      Exmpl()
      Exmpl()
      Exmpl(const Exmpl&)
      ~Exmpl()
      operator=Exmpl()
      ~Exmpl()
      ~Exmpl()
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the following code: #include<iostream> #include<vector> #include<string> using namespace std; struct Test { string
#include <iostream> #include <vector> using namespace std; typedef struct Record { std::string name; bool
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
#include <iostream> #include <vector> #include <list> using namespace std; int main() { vector<list<string> >
I've got this code: #include <iostream> #include <string> #include <vector> using namespace std; vector<string>
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index
#include <stdio.h> #include <sys/shm.h> #include <sys/stat.h> #include <string> #include <vector> #include <iostream> using namespace
Please take a look at this example: #include <iostream> #include <vector> #include <string> using
#include <stdlib.h> #include <iostream> #include <vector> #include <string> class A { public: std::string s;
Here is my code. class xyz. #include <iostream> #include <vector> #include <map> using namespace

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.