Re: To use or not to use smart pointers?
On Jul 18, 10:55 pm, "Dennis Jones" <nos...@nospam.com> wrote:
"Boris" <bo...@gtemail.net> wrote in message
news:op.tvofawbr45fww6@burk.mshome.net...
I had a 3 hours meeting today with some fellow programmers that are part=
ly
not convinced about using smart pointers in C++. Their main concern is a
possible performance impact. I've been explaining the advantages of smart
pointers endlessly (which are currently used in all our C++ software; we
use the Boost smart pointers) as I'm seriously concerned that there is a
shift to raw pointers. We are not developing system software but rather
normal Windows programs (with exceptions turned on). I wouldn't want to
write a C++ program without smart pointers any more but after that endle=
ss
discussion I wonder if I'm too strict. Any serious arguments not to use
smart pointers?
Oh my gosh, are you serious? No way. You are absolutely correct. The
benefits of smart pointers FAR outweigh any possible arguments against th=
em.
Except that used without consideration, them make the code less
reliable.
There is no performance impact (that I know of),
Depends on the application. For most applications, the
difference should be acceptable. For complicated graphs,
running in a multithreaded environment, they can double the
runtime, or worse.
and the advantages
(automatic and correct object/resource lifetime management,
You wouldn't be interested in buying this bridge I have to sell,
would you? If you believe that, God help your users.
avoidance of
memory leaks in the presence of exceptions,
In special cases (although I usually use the Boehm collector,
which takes care of the memory at far less run-time cost and
programmer effort).
to name only two) are too
compelling to ignore. Another (strange, but typical) argument made again=
st
smart pointers is their sometimes odd usage syntax (extra typing, uglines=
s?
I don't know), but in my view, that is a very small price to pay for the
peace of mind and safety afforded by their use.
The major argument against them is that they reduce the safety.
If your collegues convince you otherwise, you should be
working in another field.
If you believe that systematically using boost::shared_ptr, or
whatever, will eliminate all lifetime of object issues, you
should be working in another field.
--
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