Re: smart ptrs in vectors
On Sep 18, 1:15 am, er <erwann.rog...@gmail.com> wrote:
i'm wondering what is the role of A::operator= below in doing:
Impl impl0(0);
A a0(impl0);
std::vector<A> vec;
vec.push_back(a0);
if i put it as private the compiler complains but if i put it as
public, the message "A::operator=" does not appear. the message
"A::A(A&)" however does print.
The standard says that any object put into a standard container
must be assignable. That doesn't mean that the assignment
operator will be called for all operations on the container.
In this case, what is probably happening is that the
implementation of vector<>::push_back calls vector<>::insert.
And in certain cases (but never when inserting at the end),
insert must use the assignment operator. The reason you get the
compiler error is because the compiler is instantiating a
function which uses it. The reason you never see its output is
because it is in an if/else branch which never actually gets
executed.
Note that some implementations of the library use concept
checking, and you may get an error from the compiler any time
you instantiate the template, e.g. in the declaration, even if
you never actually do anything with the resulting instantiation.
any suggestion appreciated.
Provide a working assignment operator for all objects which are
to be in a standard container.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34