22

So basically this code:

class A {
};
class B { 
   B (const B& b) {}
public: 
   B (){}
   B (const A& a) {} 
};

int main()
{
   A a;
   B b1(a);  //OK
   B b2 = a; //Error
}

only generates an error for B b2 = a. And that error is

error: ‘B::B(const B&)’ is private

Why is it attempting to call the copy constructor in addition to the direct conversion constructor?

It's clear from the error message that a temporary B is created which is then used for copy-construction, but why? Where is this in the standard?

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Is your question related, by any chance, to [this one](http://stackoverflow.com/questions/11221242/is-this-a-copy-constructor)? :) – Eitan T Jun 27 '12 at 08:24
  • Because I've reviewed that question a few minutes ago :) – Eitan T Jun 27 '12 at 08:30
  • So to be clear, are you asking why the *compiler* is doing it (i.e. you want the standard reference for the definition of copy initialization), or are you asking why the *standard* is doing it (i.e. you want the motivation for that definition)? – Steve Jessop Jun 27 '12 at 09:22
  • 1
    @SteveJessop I'm asking for the reference first, but the motivation would also be nice to know (not gonna post a new question as it will likely get deleted). – Luchian Grigore Jun 27 '12 at 09:24
  • @SteveJessop fair. I think Jesse's comment to Als' answer is a probable explanation. – Luchian Grigore Jun 27 '12 at 09:33
  • @Luchian: I agree it's a possibility, although I think its deduction could be counter-argued. Every built-in type can be trivially copied, so I don't think it's quite true that having only the conversion wouldn't mimic the semantics -- the semantics of direct initialization and copy initialization are identical for built-in types. There's something it doesn't mimic though. I guess the problem with "motivation" questions is that people show their working for these deductions and counter-arguments in answers, when they don't have a direct source! – Steve Jessop Jun 27 '12 at 09:36
  • @SteveJessop posted new question http://stackoverflow.com/questions/11223285/whats-the-motivation-between-having-both-copy-and-direct-initialization – Luchian Grigore Jun 27 '12 at 09:37

1 Answers1

14
B b2 = a;

This is known as Copy Initialization.

It does the following:

  1. Create an object of type B from a by using B (const A& a).
  2. Copy the created temporary object to b2 by using B (const B& b).
  3. Destroy the temporary object by using ~B().

The error you get is not at step 1 but rather at step 2.

Where is this in the standard?

C++03 8.5 Initializers
Para 14:

....
— If the destination type is a (possibly cv-qualified) class type:
...
...
— Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one is chosen through overload resolution (13.3). If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the destination type. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. 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.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    But why not directly use the conversion constructor? – Luchian Grigore Jun 27 '12 at 08:25
  • @LuchianGrigore: It does.The error is *after* the conversion has been done, during copy construction.The call to copy constructor could be elided as well, but that depends on the compiler.Also, still the copy constructor needs to be accessible. – Alok Save Jun 27 '12 at 08:27
  • Yes, I got that (and I know they have to be visible regardless of whether they are called or not). But why doesn't it just do it in one step? Why the need for a temporary `B`? – Luchian Grigore Jun 27 '12 at 08:28
  • Ok, poor phrasing on my part in the question. Edited :) – Luchian Grigore Jun 27 '12 at 08:32
  • Thanks! I'd ask what the motivation behind this is, but that'd just get closed. – Luchian Grigore Jun 27 '12 at 08:44
  • 1
    @LuchianGrigore: I think the reason is to be closer to the semantics of built-in types. For example, `int i = 1.0;`: 2 things happen here. `1.0` is converted to an `int` and *then* the value is assigned to `i`, having only a conversion operator would not mimic the semantics (I believe). – Jesse Good Jun 27 '12 at 09:03
  • @JesseGood see http://stackoverflow.com/questions/11223285/whats-the-motivation-between-having-copy-and-direct-initialization-behave-diffe – Luchian Grigore Jun 27 '12 at 09:43