Pregunta

I understand how auto_ptr works in C++03. It is based on this trick. The trick uses a user-defined conversion to steal the pointer from one object to another when code such as this auto_int p(auto_int(new int())); is written. However, I've several questions in this regard.

  1. Why isn't the compiler-generated copy-ctor called?
  2. Why does the user-defined conversion take precedence over a compiler-generated copy-ctor?
  3. Is there a compiler-generated copy-ctor to begin with?
  4. If not, what language rule suppresses it?
¿Fue útil?

Solución

Why isn't the compiler-generated copy-ctor called?

Because the class already supplied a copy-constructor, a mutable one which is still valid, so the compiler does not generate a default one.

Why does the user-defined conversion take precedence over a compiler-generated copy-ctor?

There is no compiler generated copy-constructor.

Is there a compiler-generated copy-ctor to begin with?

Finally! Nope, there isn't.

If not, what language rule suppresses it?

It's 12.8.2:

A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments.

And 12.8.4:

If the class definition does not explicitly declare a copy constructor, one is declared implicitly.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top