C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Implicit conversion to T*

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Thu Aug 17, 2006 3:24 am    Post subject: Implicit conversion to T* Reply with quote



I'm trying to make an argument for why smart pointers should not provide an
implicit conversion to T*, and I would like it to be as comprehensive as
possible. Any comments and observations welcome.

Dave

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Tutone
Guest





PostPosted: Thu Aug 17, 2006 6:37 am    Post subject: Re: Implicit conversion to T* Reply with quote



d...@codelogicconsulting.com wrote:
Quote:
I'm trying to make an argument for why smart pointers should not provide
an
implicit conversion to T*, and I would like it to be as comprehensive as
possible. Any comments and observations welcome.

How about because it would defeat one of the primary purposes of using
smart pointers, which is to explicitly control ownership and thereby
avoid dangling pointers and memory leaks?

Best regards,

Tom


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Andrei Alexandrescu (See
Guest





PostPosted: Thu Aug 17, 2006 6:44 am    Post subject: Re: Implicit conversion to T* Reply with quote



dheld (AT) codelogicconsulting (DOT) com wrote:
Quote:
I'm trying to make an argument for why smart pointers should not provide
an
implicit conversion to T*, and I would like it to be as comprehensive as
possible. Any comments and observations welcome.

One pretty much canonical example is that you can lose "smartness" and
build competing smart pointers to the same object without client code
ever using one star:

SmartPtr<Tree> sp1 = ..., sp2 = ...;
sp1 = Interlink(sp1, sp2);

The client thinks they do the right thing, yet the code might be lethal
if Interlink takes raw pointers and creates a smart pointer that uses them.


Andrei

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Greg Herlihy
Guest





PostPosted: Thu Aug 17, 2006 8:58 am    Post subject: Re: Implicit conversion to T* Reply with quote

dheld (AT) codelogicconsulting (DOT) com wrote:
Quote:
I'm trying to make an argument for why smart pointers should not provide an
implicit conversion to T*, and I would like it to be as comprehensive as
possible. Any comments and observations welcome.

Consider the DoSomething() function that returns a smart pointer as its
result:

smart_ptr<T> DoSomething();

now consider this code that calls DoSomething():

T * t = DoSomething();

If a smart pointer implicitly converts to its base pointer type, then
it is perfectly legal to assign the result of DoSomething() to an
ordinary T pointer. The problem of course is that t's state is
indeterminate - it may have already been deleted - or it could be
deleted unexpectedly later on, or it could be deleted twice (as happens
when t is assigned to a new smart pointer).

Essentially, the implicit conversion makes it too easy for the T
pointer to "escape" from its smart pointer's management. Furthermore
tracking down this kind of bug can be extremely time-consuming and
frustrating - so much so that even those sold on the idea of the
convenience of an implicitly-converting smart pointer - usually change
their minds once they have built up some experience working with this
feature.

Greg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ron House
Guest





PostPosted: Thu Aug 17, 2006 9:00 am    Post subject: Re: Implicit conversion to T* Reply with quote

dheld (AT) codelogicconsulting (DOT) com wrote:
Quote:
I'm trying to make an argument for why smart pointers should not provide an
implicit conversion to T*, and I would like it to be as comprehensive as
possible. Any comments and observations welcome.

Well if you want to do that, why haven't you told us your existing
thoughts on the matter? After all, if the impulse to do this has
possessed you, you must surely have reasons for thinking so already. Or
are you just fishing for an assignment answer?

--
Ron House house (AT) usq (DOT) edu.au
http://www.sci.usq.edu.au/staff/house

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alf P. Steinbach
Guest





PostPosted: Sun Aug 20, 2006 7:06 pm    Post subject: Re: Implicit conversion to T* Reply with quote

* Ron House:
Quote:
dheld (AT) codelogicconsulting (DOT) com wrote:
I'm trying to make an argument for why smart pointers should not provide
an
implicit conversion to T*, and I would like it to be as comprehensive as
possible. Any comments and observations welcome.

Well if you want to do that, why haven't you told us your existing
thoughts on the matter?

Of course it helps (!) to have some foreknowledge of what one is
searching for, but given that, in Firefox's address line I wrote "wg21
working group", voilą, the C++ standardization committee's pages; then I
clicked on "WG papers"; and in the page of Working Group papers, I
clicked on "2004-09-pre-Redmond mailing". And there it seems that some
of the OP's earlier thoughts on the matter are laid out, in a separate
section of "N1681 04-0121 A Proposal to Add a Policy-Based Smart Pointer
Framework to the Standard Library, David B. Held".

One possible reason the OP didn't provide this reference could be that
he wanted fresh input on the subject matter.

So I suggest putting forth one's thoughts in this thread first, /then/
checking out the OP's WG paper.


Quote:
After all, if the impulse to do this has
possessed you, you must surely have reasons for thinking so already. Or
are you just fishing for an assignment answer?

Oops. ;-)

Well, unless it was a hilarious joke, but perhaps not all readers would
"get" it.

Anyway, in addition to Andrei and Greg's observations (too easy to place
that raw pointer in an unrelated smart pointer, too easy to destroy the
object prematurely by "storing" a smart pointer function result in a raw
pointer) I'd like to add consistency and clarity.

Consistency: existing quality smart pointers don't provide implicit
conversion to T*. Additional but low-impact argument: given code that's
based on a quality smart pointer QSP and that depends on detecting raw
pointers by checking for implicit conversion to T*, changing the code to
use a mediocre smart pointer MSP (heh, funny!) could break that code. I
guess there are much more likely scenarios of introducing bugs because
of broken expectations, but that one was the one my generator produced.

Clarity: implicit conversions make it less evident what goes on in the
code. Implicit conversions often serve as infection vectors for
incorrect assumptions, which in turn lead to bugs. When maintaining
existing code, a smart pointer implicit conversion to T* may yield the
incorrect impression that one is dealing with a raw pointer, which makes
Andrei and Greg's scenarios all the more likely.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
David B. Held
Guest





PostPosted: Mon Aug 21, 2006 5:37 am    Post subject: Re: Implicit conversion to T* Reply with quote

Greg Herlihy wrote:
Quote:
[...]
If a smart pointer implicitly converts to its base pointer type, then
it is perfectly legal to assign the result of DoSomething() to an
ordinary T pointer. The problem of course is that t's state is
indeterminate - it may have already been deleted - or it could be
deleted unexpectedly later on, or it could be deleted twice (as happens
when t is assigned to a new smart pointer).
[...]

This is all good, but I seem to recall there's some arguments
about accidental pointer arithmetic and other funny business
like that. Imagine you have a codebase with 1 million+ LOC...
besides the obvious leakage of ownership, what other kinds of
things can you imagine would go wrong?

Dave

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.