by lbrandy · 6y ago · 0 comments
However, you missed a _super_ important caveat of this which is very subtle and very awful. And that is all of the code being executing here between the start of the function and the copy of the shared_ptr is doing it without the protection of the reference count on the shared pointer. It's entirely conceivable that, for example, the `has_widget` function, while executing, invalidates the original shared_ptr passed in and thus makes your eventual use of that shared_ptr invalid.
shared_ptr<int> global;
void func(const shared_ptr<int>& v) {
global.reset();
*v += 1; // ohno
}
func(global);/* XXX the argument might be the same as the global value we're about to modify. API designers, eh? */
I'd also like to inquire as to what nontrivial codebase you've seen that contains no fairly stupid programming. I keep hearing about them, and trying to prove their existence, but so far I've collected more concrete evidence for the existence of bigfoot than I have for the existence of such codebases.