On 28/07/2014 01:03, Bjorn Reese wrote:
On 07/24/2014 06:53 AM, Gavin Lambert wrote:
1. shared_from_this can't be used from the constructor (requiring factory-method-init shenanigans); reportedly
If you are using C++11, then your factory can be made generic by using perfect forwarding:
While that's somewhat an improvement on manually specifying parameters, it still counts as factory-method-init shenanigans. (Only somewhat, because this sort of thing tends to confuse IDE-provided parameter-info hints, which makes it harder to call.)
And it should be possible to obtain that weak_ptr before any shared_ptr exists, and "fix" it later to point at the real shared_ptr, especially if it is known that the weak_ptr will never actually be used until that real shared_ptr exists.
This sounds a bit risky if the last assumption does not hold.
It's not any more risky than the current behaviour of shared_from_raw. There's nothing incomplete or invalid about the weak_ptr thus obtained, and there's no reason why other code couldn't actually use it or even lock it back to a shared_ptr and save that somewhere. It just points at the internal self-cyclic pointer, which will create a memory leak if it's not cleared by having code outside of the constructor assign the raw pointer to a "real" shared_ptr. As soon as that occurs, everything is happy again. But at no point is anything invalid or unusable.