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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:26:46+00:00 2026-05-23T10:26:46+00:00

If I write T t = T(); T is a class. I think this

  • 0

If I write

T t = T();

T is a class.

I think this is calling T’s default constructor and then the copy assignment operator.
But the compiler is allowed to get rid of the assignment.

I’m trying to find the description of this behavior written in the C++ standard, but I can’t find it.
Could you point me to the right spot in the standard?

I’m asking this because I’m being asked to replace this :

T t;

with

T t = T();

because of a coding rule checking program.

and it happens that the T class is noncopyable and has a private copy constructor and copy assignment operator…
So I’d like to see that the compiler is effectively always getting rid of the copy in this case.

edit:
I have been mislead by something weird:
the noncompyable class was actually inheriting from boost::noncopyable
in this case it does compile.
But if I declare the copy constructor and copy assignment operator private, it does not compile.
exemple. This compiles :

class BA
{
protected:
    BA() {}
    ~BA() {}
private:
    BA( const BA& );
    const BA& operator=( const BA& );
};

class A : BA
{
};

int main( void )
{
    A a = A();
    return 0;
}

and the following does not :

class A
{
public:
    A() {}
    ~A() {}
private:
    A( const A& );
    const A& operator=( const A& );
};

int main( void )
{
    A a = A();
    return 0;
}
  • 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-23T10:26:46+00:00Added an answer on May 23, 2026 at 10:26 am

    Since you asked for the C++ standard citation, here it is:

    12.8 copying class objects

    15

    *When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if the copy constructor and/or destructor for the object have side effects. In such cases, the implemen-tation treats the source and target of the omitted copy operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects
    would have been destroyed without the optimization.111)This elision of copy operations is permitted in the following circumstances (which may be combined to eliminate multiple copies):

    — in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type, the copy operation can be omitted by constructing the automatic object directly into the function’s return value.

    — 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*

    Example:

    class Thing
    {
        public:
           Thing();
           ˜Thing();
           Thing(const Thing&);
    };
    
    Thing f() 
    {
        Thing t;
        return t;
    }
    
    Thing t2 = f();
    

    Here the criteria for elision can be combined to eliminate two calls to the copy constructor of class Thing:
    the copying of the local automatic object t into the temporary object for the return value of function f() and the copying of that temporary object into object t2. Effectively, the construction of the local object t can be viewed as directly initializing the global object t2, and that object’s destruction will occur at program exit.

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

Sidebar

Related Questions

I want to write class whose constructor takes two parameters, but the arguments are
Suppose, I write class A { }; The compiler should provide (as and when
In my team I've been told to write resource class like this style: class
By default Html.ValidationSummary() produces HTML like this: <span class=validation-summary-errors>There were some errors...</span> <ul class=validation-summary-errors>
I can write some links: <p>This value is 23456. <a class=edit href=# custom-data=2356>Edit</a></p> <p>This
When I write class templates, and need to fully-specialize members of those classes, Doxygen
Write a class ListNode which has the following properties: int value; ListNode *next; Provide
When I write a class I always expose private fields through a public property
I'm trying to write a class in Delphi 2007 that uses a ActiveX library.
Would it be possible to write a class that is virtually indistinguishable from an

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.