Why can't weak pointers access the main pointer?

I ask this because it is a debilitating limitation for a number of obvious reasons, but I'm sure the standard C ++ guys had good reasons for this, and I would like to know what it is. Not because it will make any difference, but because it will make me feel better, knowing that there is a good reason for this.

Here is my current activity; as I can see, weak pointers have 2 main uses:

1) to avoid matches. 2) to reduce the overhead associated with rolling back around multiple common pointers.

The former will be serviced no less, and the latter will be better served by implementing weak_ptr.get (), so why doesn't it exist? Moreover, would weak_ptr.get () not be more efficient than weak_ptr.lock (). Get (), or can the compiler optimize this to be the same?

+6
source share
3 answers

A weak pointer should be able to check whether the correct pointer that it refers to is valid. If he could not do this, then you could not know if the result from get valid by the time you used it.

This is why lock ; it gives you a smart pointer that will be valid as long as you need it (which is not expected for a very long time).

If you need direct access, then what you really want is the usual dumb pointer.

+9
source

1) to avoid matches. 2) reduce the overhead associated with bouncing around a set of common pointers.

The former will be serviced no less, and the latter will be better served by implementing weak_ptr.get (), so why doesn't it exist? Moreover, would weak_ptr.get () not be more efficient than weak_ptr.lock (). Get (), or can the compiler optimize this to be the same?

The former is true, the latter is false - weak pointers are not magical, inexpensive, general pointers ... they are functionally different. You need to choose the one that suits your situation.

The main reason for using weak pointers is to keep track of some of the data that you might want to use if it still hangs around but can survive without pleasure. An establishment that is connected with the state of the race with users of shared pointers, so if you do not have insider information about the lifetime of the object, this means that you can be sure that your code can complete before the destruction of the object, an unprocessed pointer will be useless. If you have insider insight, you can get the original pointer initially - instead of a weak pointer, but the design of the weak pointer is designed to prevent you from accidentally getting it and creating unsafe code. In fact, any such understanding may be fragile in the face of the medium / long term evolution of the code, so it is probably best not to try to do such optimizations if you shouldn't.

Perhaps a good example of using a weak pointer will help ...

Say that the object represents something on the screen - if you want it to start, you added a weak pointer to the list of flashes with a timer, then you can forget about it. When the timer is triggered, if the Flasher code detects that the object was destroyed in advance (i.e., it is no longer on the screen), flasher only removes it from the list, but it does not want to share property and not support the object unnaturally. It might be easier than trying to track code when an object disappears from the screen to tear it out of the flasher list (and possibly block its lock for this).

+1
source

This is due to the fact that the developers of the shared_ptr / weak_ptr pair decided that it was better to save us from ourselves and to ensure that we could not use it incorrectly due to optimizations, when we know that it does not matter. It may not be necessary for the semantics of thread safety and ownership to be the same - it is very simple to create a single block with code, where there is a good smart pointer that knows that its zero is useful, but the overhead of locking, although not exhausting is definitely pessimization.

Whether shared_ptr / weak_ptr should allow this (adding say notad_ptr, where notad is null to kill the target but not necessarily thread safe), or whether this should be the new type shared_ptr, is debatable.

We solve this in our code base by adding a new type of smart pointer that supports these semantics. However, this is definitely not optimal, since the presence of non-standard smart pointers is definitely annoying, but the additional useful semantics more than compensate for the fact that we are probably not so smart [pun intended!], Since the full standardization committee :)

0
source

Source: https://habr.com/ru/post/947959/


All Articles