0
votes

Quite often I work with an old code where raw pointers are mixed with smart ones and I don't have the time to change all raw ones to smart.

And there can be some situations like static raw pointer pointing to an object, which can be already destructed and at first it seems like a situation to use weak_ptr to hold the reference, but there the problem arises, because the place with raw pointer does not have any information about shared_ptrs already pointing to the same object.

So:
1) Is there any smarter smart pointer that tracks all pointers (both raw and smart) to an object?
2) Is there any smarter smart pointer that at least tracks all shared_ptrs to an object?

I don't want a discussion about an implementation, if possible I want to use it as a black-box.

EDIT: I asked 2), because for example calling make_shared on an object twice, makes 2 separate shared_ptr reference counters.

2
Short answers: no and no.Sam Varshavchik
Just think for a second: How would you implement such a smarter smart pointer? How do you catch it when a member function returns a pointer/reference to one of its members? How do you catch it when a member function returns this? If you answer like me "that's impossible to do", well, you got the reason why no such things exists...cmaster - reinstate monica
(1) is surely impossible. Can you elaborate on (2)? Do you want to get a list of all shared_ptrs to an object given... a raw pointer to it? Or a shared one?HolyBlackCat
Sounds like you want objects to be self-aware that they are managed by a smart pointer (i.e., a std::shared_ptr). That's what std::enable_shared_from_this does. Note that some people (myself included) consider this an anti-pattern, but it is part-and-parcel of the C++ language by the standard.Eljay
Note that if you have Foo foo; and then do auto ptr = std::make_shared<Foo>(foo); you have a pointer to a copy of foo, i.e. a separate objectCaleth

2 Answers

2
votes

1) Is there any smarter smart pointer that tracks all pointers (both raw and smart) to an object?

No. There is no such standard pointer and such pointer is not implementable in standard C++.

2) Is there any smarter smart pointer that at least tracks all shared_ptrs to an object?

It is somewhat unclear what you mean. Ownership is implicitly shared with all copies of the shared pointer. If you mean that you want taking ownership with separate shared pointers to work, there is no such smart pointer in C++. Maybe implementable with a global data structure.

On the other hand, std::enable_shared_from_this might be what you're looking for instead. It still doesn't work if you try to take shared ownership separately, but it provides a convenient way to join an existing ownership without needing access to any of the shared pointers.

1
votes

You may want to program in a language not based in any way shape or form on C.

C has pointers. Smart pointers are not real pointers.

Unless you invent a different language, not compatible at any level with C, you will always have pointers and these won't be under the control of any so called "smart pointer", which is neither smart nor a pointer.