Conversation started Jul 13, 2011 at 9:29.
Jul 13, 2011 09:29
1
A: When should I use C++ pointers over Smart Pointers?

Matthieu M.The use of smart pointers is always recommended because they clearly document the ownership. What we really miss, however, is a "blank" smart pointer, one that does not imply any notion of ownership. template <typename T> class client_ptr { public: client_ptr(T* p): _p(p) {} template ...

What do you guys think of this answer?
I think it's kind of silly. A smart point which isn't smart?
a dumb_ptr
The point in smart pointers isn't just that they document ownership, they define ownership. If you don't want any ownership, you use a raw pointer. I don't really see the point in contrived "smart-dumb-pointers" just to document that "I don't want any ownership, thankyouverymuch"
@jalf you have a valid point there, I agree
Funny stuff, I did write a pointer<T> helper.
Jul 13, 2011 09:40
well in a large work environment with many contributors I could see the benefit of being explicit like that.
I always thought of naked pointers as documenting that already: "I don't want any fancy schamcy ownership things, just gimme the address, mkay?"
Although it was more for move semantics, I don't mind using raw pointers when that is not an issue.
@MartinhoFernandes yeah, exactly
the problem with using some dummy ptr<T> to "document" it is that it's no more reliable than just using a raw pointer. It doesn't guarantee that no ownership is intended. The type might be just as misleading as the variable name
nothing stops you from taking a pointer to dynamically allocated memory that you "own", and wrapping the pointer in your dummy "non-owning" pointer. You still own the object, and you still have to delete it. Wrapping the pointer in a "non-owning pointer wrapper" didn't change that. It just served to obfuscate your code
well variable names don't guarantee anything either, it's just self-documenting code
@Kometes yeah, but that's my point. There's nothing gained by inventing a new type to make the distinction, if the type isn't capable of enforcing the distinction
Jul 13, 2011 09:43
Oh no, why would you suggest an abbreviation. pointer<T> is so much less of an eyesore than ptr<T>.
@jalf my question is, how do you create a wrapper that is a 'non-owning' wrapper? I mean, it will always own the raw ptr, no?
a shared_ptr enforces that "when the ref counter reaches 0, this object will be destroyed". No way around it. This hypothetical pointless_ptr<T> doesn't enforces non-ownership, so it's, well, pointless
@TonyTheTiger owning means taking care of deletion
@TonyTheTiger it'll always own the raw ptr, sure, but it doesn't own the object being pointed to
which is what smart pointers do
shared_ptr has exclusive ownership of its internal raw pointer too, but that's not interesting. What matters is its shared ownership of the "pointee"
@jalf ah, and that's cause smart pointers determine the objects destruction?
Jul 13, 2011 09:45
@jalf Actually you're wrong, you can't misuse ptr<T> since there is no get member.
Can't do delete p.get(). What more would you want to enforce?
@LucDanton can do delete &*p
So can you with any sort of pointer, owning or not. IMO this is an instance of protecting against Machiavelli rather than Murphy.
(or the other way around, it's too early yet)
I won't use such a template because I use the convention that a raw pointer is either an iterator and/or a non-owning pointer but I can see how it could be useful in an old/mixed codebase.
@LucDanton My problem is that I wouldn't trust such a template. It'd be too easy to accidentally convert an "owning raw pointer" into an "owning non_owning_ptr"
Could this be done in C++11: template <typename T> using ptr<T> = T*;?
I trust a shared_ptr because it enforces its ownership. I can't trust a non_owning_ptr, because it doesn't.
Jul 13, 2011 09:51
If you work with people that are ready to do delete &*p you'll have problems elsewhere anyway.
@MartinhoFernandes I don't see why not.
but it's a type synonym
You could set up a script that looks for delete &* (modulo spaces and parentheses) if you're that paranoid I guess.
As is that pointer has some drawbacks. It's not LessThanComparable, for starters.
@LucDanton my problem is more at the other end. What's to stop someone from accidentally (or even implicitly, because of changes elsewhere in the code) wrapping an owning pointer in this wrapper? Then the user will assume that it doesn't own anything, and refrain from calling delete
resulting in a memory leak
Seems more like a migration issue than a problem with the template itself.
because by wrapping your pointer in this, you made a promise you didn't keep: that the object is owned by someone else, and will be destroyed by someoen else
wait no that's stupid
Jul 13, 2011 09:55
@LucDanton Absolutely. But it punches a big hole in the one use case you had for the template
Well
If the pointer is owning
somewhere there is a delete p
You're introducing it on the premise that "as long as we all write correct code and have a clear notion of ownership, it'll work fine". And then you're going to use it to solve all the problems resulting from a lack of a clear notion of ownership
If whoever is replacing that with a ptr<T> can't tell there's a problem here well
How is it an owning pointer if it is never deleted?
(or, recursively, passed to somewhere where it is deleted)
@LucDanton logically owning (as in, delete should be called on this pointer), but not semantically (we forget to actually call delete on it)
as soon as someone write &*p it's quite visible what's going on
Jul 13, 2011 09:57
@LucDanton but what if the pointer was non-owning when I wrote the code to wrap it in a ptr<T>, but then a week later, someone changes some library code so that it becomes owning?
If you work with people that are likely to do this the problem is with the workplace, not the template.
@jalf That someone will have to write &*p somewhere along the line. Don't allow this
it still gets wrapped in a ptr<T>, and now it looks safe to users, because "hey, it's a ptr<T>, someone must have made sure the ownership semantics are correct", but it isn't
@LucDanton no, I'm saying someone might have changed the semantics of the original raw pointer
before it was wrapped in a ptr<T> .
say you call some third party API which returns a pointer
Maybe the API wasn't clearly documented, so what you assumed was non-owning turns out, much later, to have been "logically owning" all the time. You were supposed to call delete on it, but you never did so
Then don't wrap it. At some point something will want to reclaim a T* and ptr<T> will stumble upon that.
and now, you've further obfuscated your code by wrapping this in a ptr<T>
@LucDanton will they? Why? What makes you assume that?
Oh wow.
That's a terrible argument, no offense intended.
The code is already wrong. No amount of false confidence makes it less wrong.
Jul 13, 2011 10:00
And how do I know that "2 months from now, I'm going to discover that the pointer I thought was non-owning is actually owning. I'd better not wrap it in a ptr<T>
@LucDanton Exactly. That's my point. If you can't safely use the template in wrong code, then it's useless in catching errors (because errors, by definition, only occur in wrong code)
I'd like to point out that neither Matthieu or I suggested that the pointer be used for migration.
using it in correct code is a waste of time, because in correct code, the ownership semantics are already clear
When I referred to a mixed codebase I meant that the newer parts would never be written with raw pointers, only owning smart pointers or ptr<T>.
but the newer parts will still have to interact with the older parts. Where the ownership semantics aren't clear cut, and calling a function in the older part might return a raw pointer whose ownership semantics aren't well-documented
Jul 13, 2011 10:03
do you call delete on that? Do you wrap it in a ptr<T>? Do you leave it as a raw pointer?
I just don't see it solving any problems
If it's not well documented you can't do anything with it.
If we assume that "I already know the ownership semantics of my raw pointers", then I don't need to wrap them in this. And if I don't know the ownership semantics, then I'm screwed in either case, and I just end up obfuscating my code even further
@ÓlafurWaage hey
Look, I understand that bridging the impedance mismatch between codebases is hard and tedious. That there are a lot of problems. But nobody suggested that ptr<T> is a panacea. You're blaming a lot the hardships involved on this template when it's just what's at hand that is not easy.
And sometimes "we're not going to write new code with T*" is the problem to solve.
Add bits to the old codebase with T* if you want and make it count as maintenance.
But the newest Foo and Bar modules will use state of the art ptr<T>! And chrome!
How will you pass ptr<T> to the old void qux(T* fred); function?
I won't
It's to be used internally.
Not at the interface.
i.e. the modules are not here to wrap the interface, it's added functionality.
Jul 13, 2011 10:11
But you need to qux the freds. And that's in the old codebase. The new modules are not independent.
@LucDanton no, I'm saying that I want my documentation to be correct. There are few things worse in programming than encountering code that makes promises it doesn't keep.
Good point!
Since you have no way to prevent a "pointer which should be deleted eventually" inside a ptr<T>, I feel that it solves no real problem, while potentially making promises that look exceedingly trustworthy (because you encode it into the type system), but which can still be wrong just as easily as a code comment or a variable name
@jalf The 'problem' is identifying which bits are the new fangled code and which bits are the old codebase. And as Martinho points out, some new bits that interface with the old codebase will still use T*.
The point is that if I see T*, I know it's from/interface with the old codebase.
If I see ptr<T>, then proceed as usual.
But the ptr<T> template has no way to convert it back to T*! In other words ptr<T> is not backwards compatible.
Jul 13, 2011 10:13
Use a comment. //this pointer comes from the old codebase, watch out seems like it'd work just as well
but without the false sense of security you get by abusing the type system
Yeah; firewall the bits of Foo that interface with the old codebase big // WARNINGS and use T* in those bits.
@LucDanton and then use T* elsewhere without the warning
@jalf Or use ptr<T>
You can't pass ptr<T>s to the old code.
but why? it doesn't buy you anything that I can see
Jul 13, 2011 10:15
Not every problem is hard and needs a mind-blowing solution.
It's self-documenting: 'oh, this has been written in the new style and is non owning and doesn't touch the old codebase'
@LucDanton no it isn't
FWIW you can write std::shared_ptr { some_interface, whoops_this_is_the_wrong_deleter }
once again, you have no guarantee that a ptr<T> is actually logically non-owning
std::shared_ptr doesn't enforce that you have the right semantics either.
you just have to HOPE that whoever wrapped it knew what he was doing
And I hate hoping when I program
Jul 13, 2011 10:16
you just have to HOPE that whoever wrapped it knew what he was doing.
@LucDanton sure it does. It'll destroy the program automatically, on its own
er
the object, not the program
;)
No it doesn't.
Check this
it can't prevent you from also deleting the pointer yourself, sure, but you'll generally notice when that happens
Jul 13, 2011 10:18
Old interface: fred* get_fred(); void dispose_fred(fred*);
I wrap it: std::shared_ptr<fred> p { get_fred() }
once you have a shared_ptr, you know that "whatever else anyone does with the object being pointed to, the shared_ptr will carry out the semantics it promised"
Then someone else, lulled into a false sense of security, takes a copy of p
But it promises nothing
Are you serious?
std::shared_ptr<fred> p { get_fred(), noop_deleter }
You can copy and move p around all you want
@LucDanton Yes? that promises that "when the ref counter reaches zero, noop_deleter will be invoked
At the end of the day, someone fucked up and it's all wrong anyway.
That's a silly argument.
Jul 13, 2011 10:20
@jalf Should have called dispose_fred! BOOM
I was making a parallel. Was I clear?
sort of. It doesn't make sense, but I get what you were trying to point out
> I feel that it solves no real problem, while potentially making promises that look exceedingly trustworthy (because you encode it into the type system), but which can still be wrong just as easily as a code comment or a variable name
I agree it doesn't make sense.
Demonstratio ad absurdium.
When I wrap something in a shared_ptr, I enforce some semantics. if I provide a custom deleter at the time, I'd better make sure the deleter is correct, but I get a guarantee that the deleter will be called when the shared_ptr determines that it is time to do so
Wee, tracing bugs across time and space.
@jalf so a correctly created shared_ptr keeps correct when copied and moved around
Guess what happens with a ptr<T> at the correct place?
Jul 13, 2011 10:21
@LucDanton no, there is no parallel. You might as well use "look, the sky is all green today, and I saw a pig fly by" as your "parallel" and then claim "ad absurdium"
@LucDanton the same as would happen with T* at the correct place.
I like the uneasy feeling of insecurity naked pointer syntax gives you. Lack of splats in the type gives me the exact opposite idea.
@jalf Reduction ad absurdium means that I come at a wrong conclusion from a wrong premise. Not that anything was absurd or green...
@LucDanton yes, and my point is that your premise had nothing to do with my premise, so it says nothing about my conclusion
@jalf Yes; but the point of ptr<T> would be to distinguish those parts that interface with the old and those that are new.
@LucDanton For that, my simple template alias would do just fine.
Jul 13, 2011 10:23
If I wrap something in a shared_ptr, I impose the semantics that I want. If I wrap something in a ptr<T>, I make impose nothing, but I make others think that "the semantics are going to be like this", whether or not that's actually true
You don't see how a shared_ptr looks trustworthy but enforces no safety?
@MartinhoFernandes Possibly yes.
@LucDanton not really, no
@jalf If I write a correct use of shared_ptr, anyone who uses it carry on with correct semantics
I never said anything about correct semantics. (or even "safety"
)
If I write a correct use of ptr<T> how can it go wrong?
Jul 13, 2011 10:25
Just that I'm guaranteed that a shared_ptr provides the promised semantics
That was a shorthand for that.
If I write ptr<T> p and fulfill the promise that it really is non-owning where is the problem?
Yes, I can lie and write a ptr<T> that is not really non-owning.
Just the same as with shared_ptr<T>
A shared_ptr guarantees that "I'm going to call my deleter when it's time". If that's the wrong deleter, you screwed up. If you hang on to a reference forever, it also won't save your code. But the promises made by the shared_ptr will be kept
The promise made by ptr<T> (that "you shouldn't worry about deleting this pointer") may or may not be kept
see the difference?
Nope.
(allow me time to read and reread that)
@jalf Isn't that the same promise T* makes?
Yeah no I don't see it. If I'm using a ptr<T> why should I suddenly worry about deleting it?
Jul 13, 2011 10:28
@LucDanton maybe you were supposed to. Maybe it was erroneously wrapped in a ptr<T>
you don't know
Just the same I can't know whether the original shared_ptr has the right deleter!
@MartinhoFernandes exactly. Which is why I'm saying you might as well just use T*
I don't know indeed.
@LucDanton no, but you can know that the shared_ptr will behave like a shared_ptr.
And a ptr<T> won't behave like a ptr<T>? What does that mean?
Jul 13, 2011 10:30
@LucDanton that a ptr<T> might wrap a pointer which you should call delete on
@LucDanton how do I know that you fulfilled the promise? (or more to the point, how come I can I trust you to fulfill your promise with a ptr<T>, but not with a T*?)
and a shared_ptr might wrap the wrong deleter. How do I know that the promise was fulfilled?
Oh I get it.
We're in violent agreement.
If you're worried about the custom deleter passed to a shared pointer, you can just grep for anywhere a shared pointer is created with a custom deleter, and doublecheck that. That's easy enough to do.
Yes, ptr<T> is equivalent to T*. The one thing it buys it's a separate type.
@LucDanton yes, but a separate type which might actually not mean what you think it means
and so, I'd prefer not having it
I won't argue with your preferences :)
But seriously
why do you say things like
> yes, but a separate type which might actually not mean what you think it means
I can say that about shared_ptr.
Jul 13, 2011 10:32
If I see a non_owning_ptr<T>, then I want to be able to assume that "this is a non-owning pointer". if I have to consider "maybe someone screwed up, and this is actually an owning pointer that someone just forgot to wrap in a smart pointer", then I might as well stick with a T*, which is at least honest about it
It's not honest at all.
It's the same problem. "Is it really owning? Really non-owning?"
@LucDanton yes, but it doesn't pretend that "you can rest assured no ownership is associated with this pointer"
You can't rest at all around a possibly owning raw pointer.
@LucDanton exactly
Let me recap
The scenario I provided for ptr<T> was distinguishing new code from old code.
Jul 13, 2011 10:35
but you can't rest around your ptr<T> either, because it's subject to the same weaknesses, and yet it is designed to pretend to you that those weaknesses do not exist
I never claimed it's more easy to use than T*; that would be a silly claim.
@LucDanton But that sounds like a silly goal to me. What about the new new code?
@LucDanton and I don't buy it. Old code needs to call new code, and new code needs to call old code. You'll still have raw pointers around the place, and they'll still need to be passed back and forth.
The only gain was that it's a separate type that does the same thing.
Yes, the interface would need T*.
Would you write ptr2<T>?
Jul 13, 2011 10:36
You either know what their ownership semantics are, in which case you're safe and all is well, or you don't, in which case you're screwed, and ptr<T> only lets you pretend that you know what the ownership semantics are
@MartinhoFernandes Might as well name them, foo_ptr<T>, bar_ptr<T>, ...
@LucDanton I disagree. A shared pointer guarantees to call its custom deleter. It does exactly what it says on the box*. If you pass a wrong deleter to it, it'll call the wrong deleter. Just like if you pass the wrong pointer to it, it'll delete the wrong pointer. But it enforces a relationship between the pointed-to object and the deleter
anyway, lunch time
@jalf I only care to know if the pointer is internal to the new codebase or comes from/interface with another one.
What it enforces is that it won't mix with another codebase
Heh that sounds like a shady definition of 'enforce'
What prevents you from writing T* in the new codebase?
I guess that if there is an interface to an old codebase that returns a long* it's problematic to use ptr<long>
ptr<long>(p); // where did that long* p come from?
Seems not that convenient to only use factories if I really, really want to enforce by the type system.
Jul 13, 2011 10:40
The way I see it, by making them different types, you're basically artificially creating a mismatch between the two codebases.
@MartinhoFernandes Yes
Why make things harder?
Well; that should be the answer really.
I really like to use types because I like types and the type system.
T* is a part of the type system.
(I wouldn't go out of my way in this particular case to disallow ptr<T>(T*) though)
Of course it is.
So is void*
We don't use void* everywhere do we.
I use types because the type system is a nice feature.
Jul 13, 2011 10:42
And we don't use T* everywhere either.
Only where it makes sense.
And I provided a scenario which, IMO, would make sense.
T* for the old codebases and at the interface, ptr<T> + smart pointers for the new stuff.
I'm not saying it should be done this way, but I think it's not unreasonable to do it.
And if the old codebase was already using T* + smart pointers?
Would it shine a new light on my position if I told you I really, really want a type soandso = int; feature (aka strong typedef)?
@MartinhoFernandes Non owning T* or mixed?
@LucDanton Correctly-used T*, aka, non owning.
Then no.
 
Conversation ended Jul 13, 2011 at 10:45.