I see. For on on the other hand, I have a very small program that prints a picture using Boost::GIL, and while the program is essentially trivial, it takes about 10 sec to compile. When I switched to PCH, I got 50MB worth of compilate, and the actual compilation now takes a fraction of a second
That's good for testing and recompiling :-)
Anyway, I finally figured out how to integrate those guys into cmake.
@Kometes yeah, but it was never really realistic in the C++11 timeframe. To be honest, I was pleasantly surprised that they showed interest in picking it up post-C++11.
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 ...
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"
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
@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
a shared_ptrenforces 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
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"
@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
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
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
@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?
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
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)
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
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
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!
@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.
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.
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"
> 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
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
@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.
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
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
@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*?)
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.
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
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
@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.
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
@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
> By Milestone 3, they had added an interesting block to the main form's load event. At each launch, the application would attempt to run a series of hard-coded ALTER statements against the database, e.g. add a column here, create a table there, drop this index and create that one. If any execptions were thrown, it just assumed that those changes must have already been done, and it simply ignored any exceptions.
Why do I keep being surprised by the amount of stupidity programmers are capable of?