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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:30:51+00:00 2026-05-25T06:30:51+00:00

In the book Generic Programming and the STL (Chinese edition), it says: X x

  • 0

In the book Generic Programming and the STL (Chinese edition), it says:

X x = X() will call the copy constructor.

It seems a little weird to me. And I write a test program like this

#include <iostream>

class Test {

public:

    Test() {
        std::cout << "This is ctor\n";
    }

    Test(const Test&) {
        std::cout << "This is copy-ctor\n";
    }

};

int main(int argc, char** argv)
{

    Test t = Test();
    return 0;
}

The output is “This is ctor”. ok, now I’m confused, which is right?

  • 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-05-25T06:30:51+00:00Added an answer on May 25, 2026 at 6:30 am

    Nomimally yes, a temporary is default-constructed, and then the copy constructor is invoked to copy it into your object t.

    However, in practice the copy can be optimised out — even though it has side effects (the console output):

    [n3290: 8.5/16]: [..] In certain cases, an implementation is
    permitted to eliminate the copying inherent in this
    direct-initialization by constructing the intermediate result directly
    into the object being initialized; see 12.2, 12.8.

    And (in conjunction with the example given in the same clause):

    [n3290: 12.2/2]: [..] An implementation might use a temporary in
    which to construct X(2) before passing it to f() using X’s copy
    constructor; alternatively, X(2) might be constructed in the space
    used to hold the argument. [..]

    But the copy constructor does still have to exist, even though it might not be invoked.

    Anyway, if you compile with optimisations turned off (or, with GCC, possibly -fno-elide-constructors), you will see:

    This is ctor
    This is copy-ctor
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My os-book says that if you want to add a system call to the
I'm reading book the C# programming Language, 4th Edition, by Anders Hejlsberg etc. There
I am using Django generic View but when i type /book/edit/9/ Then it says
The book that I purchased to help with my SSIS understanding seems to have
A) Book I’m learning from says that if we handle Login.Authenticate event, then we
The book says about a small Windows.Forms program The Windows Forms classes are in
In the Java Generic Book, while contrasting the difference between C++ Templates and Java
I've got NHibernate -based (constructor ISessionFactory injection) generic repository implementation which is stored inside
From Jon Skeet's wonderful book C# In Depth, First Edition : class Film {
I have some confusion about generic programming in java: If Manager is subclass of

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.