J
Jerry Coffin
[ ... ]
But you _do_ want the smart pointer to add some intelligence. You want
it to implement copy on write, so the copies involved in putting the
item into the container are cheap, but the object still gets destroyed
when there are no more references to it.
The smart pointer should (normally) eliminate the requirement for a copy
constructor in the underlying object. The most you typically do is
declare a (private) copy constructor, but never implement it. This
allows the compiler to diagnose any code that does anything that would
result in the expensive copies you're trying to eliminate.
But in my specific case, there is no complex resource management to speak
of: an object is part of the map and that's all. The use of smart pointers
is just a work-around for the fact that the gcc implementation of std::map
does two gratuitous copy operation when inserting via operator[]. Not a big
problem here: my objects are tens to hundreds of kB, therefore the use of a
smart pointer is negligible. But in general I don't see why you would have
to use smart pointers when there are no smarts required, or why you have to
implement a copy constructor when all you do is just insert into and erase
from a map, but never copy the map or anything like that. Keeping copy
constructors up-to-date is tedious and error prone after all.
But you _do_ want the smart pointer to add some intelligence. You want
it to implement copy on write, so the copies involved in putting the
item into the container are cheap, but the object still gets destroyed
when there are no more references to it.
The smart pointer should (normally) eliminate the requirement for a copy
constructor in the underlying object. The most you typically do is
declare a (private) copy constructor, but never implement it. This
allows the compiler to diagnose any code that does anything that would
result in the expensive copies you're trying to eliminate.