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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:47:10+00:00 2026-05-22T18:47:10+00:00

I was reading the difference between direct-initialization and copy-initialization (§8.5/12): T x(a); //direct-initialization T

  • 0

I was reading the difference between direct-initialization and copy-initialization (§8.5/12):

T x(a);  //direct-initialization
T y = a; //copy-initialization

What I understand from reading about copy-initialization is that it needs accessible & non-explicit copy-constructor, or else the program wouldn’t compile. I verified it by writing the following code:

struct A
{
   int i;
       A(int i) : i(i) { std::cout << " A(int i)" << std::endl; }
   private:
       A(const A &a)  {  std::cout << " A(const A &)" << std::endl; }
};

int main() {
        A a = 10; //error - copy-ctor is private!
}

GCC gives an error (ideone) saying:

prog.cpp:8: error: ‘A::A(const A&)’ is private

So far everything is fine, reaffirming what Herb Sutter says,

Copy initialization means the object is initialized using the copy constructor, after first calling a user-defined conversion if necessary, and is equivalent to the form “T t = u;”:


After that I made the copy-ctor accessible by commenting the private keyword. Now, naturally I would expect the following to get printed:

A(const A&)

But to my surprise, it prints this instead (ideone):

A(int i)

Why?

Alright, I understand that first a temporary object of type A is created out of 10 which is int type, by using A(int i), applying the conversion rule as its needed here (§8.5/14), and then it was supposed to call copy-ctor to initialize a. But it didn’t. Why?

If an implementation is permitted to eliminate the need to call copy-constructor (§8.5/14), then why is it not accepting the code when the copy-constructor is declared private? After all, its not calling it. Its like a spoiled kid who first irritatingly asks for a specific toy, and when you give him one, the specific one, he throws it away, behind your back. 😐

Could this behavior be dangerous? I mean, I might do some other useful thing in the copy-ctor, but if it doesn’t call it, then does it not alter the behavior of the program?

  • 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-22T18:47:11+00:00Added an answer on May 22, 2026 at 6:47 pm

    Are you asking why the compiler does the access check? 12.8/14 in C++03:

    A program is ill-formed if the copy
    constructor or the copy assignment
    operator for an object is implicitly
    used and the special member function
    is not accessible

    When the implementation “omits the copy construction” (permitted by 12.8/15), I don’t believe this means that the copy ctor is no longer “implicitly used”, it just isn’t executed.

    Or are you asking why the standard says that? If copy elision were an exception to this rule about the access check, your program would be well-formed in implementations that successfully perform the elision, but ill-formed in implementations that don’t.

    I’m pretty sure the authors would consider this a Bad Thing. Certainly it’s easier to write portable code this way — the compiler tells you if you write code that attempts to copy a non-copyable object, even if the copy happens to be elided in your implementation. I suspect that it could also inconvenience implementers to figure out whether the optimization will be successful before checking access (or to defer the access check until after the optimization is attempted), although I have no idea whether that warranted consideration.

    Could this behavior be dangerous? I
    mean, I might do some other useful
    thing in the copy-ctor, but if it
    doesn’t call it, then does it not
    alter the behavior of the program?

    Of course it could be dangerous – side-effects in copy constructors occur if and only if the object is actually copied, and you should design them accordingly: the standard says copies can be elided, so don’t put code in a copy constructor unless you’re happy for it to be elided under the conditions defined in 12.8/15:

    MyObject(const MyObject &other) {
        std::cout << "copy " << (void*)(&other) << " to " << (void*)this << "\n"; // OK
        std::cout << "object returned from function\n"; // dangerous: if the copy is
          // elided then an object will be returned but you won't see the message.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the difference between reading a value from an SqlDataReader using this syntax: Dim
Does anyone have any idea (numbers?) that shows the difference between reading image files
I have been reading about the differences between Table Variables and Temp Tables and
After reading a question on the difference between pointers and references , I decided
Possible Duplicate: Difference between pointer variable and reference variable in C++ I am reading
What is the difference between a belongs_to and a has_one ? Reading the Ruby
I got the difference between Bind and EVal from here . Bind will do
I don't get the difference between System.ServiceProcess.ServiceProcessInstaller and System.ServiceProcess.ServiceInstaller . From metadata: the first
Is there a difference between directly downloading a file from a web server, and
I'm trying to understand the difference between OnStart() and the constructor in a ServiceBase

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.