On Sunday, 15 October 2017 10:45:21 UTC+3, Paavo Helde wrote:
> On 15.10.2017 3:44, Öö Tiib wrote:
> > On Saturday, 14 October 2017 23:24:08 UTC+3, Vir Campestris wrote:
> >> On 14/10/2017 14:27, Öö Tiib wrote:
> >>> When someone constructs shared_ptr from raw pointer (without
> >>> also providing deleter) then I feel that it strongly smells like
> >>> newbie and is potentially copy-paste from stackoverflow or other
> >>> newbie site. Can anyone bring sane counter-example?
> >>
> >> Yes. If the constructor is private to force use of a factory function
> >> make_shared won't compile. (I tried and failed to work out the correct
> >> friend statement, then gave up)
> >
> > What? Enforcing additional allocation and additional deallocation
> > just to force use of factory function? In general such schemes feel
> > like "I am schizophrenicly fighting with myself".
>
> This thread is about smartpointers. If the object is not dynamically
> allocated, then there is no need for the smartpointer in the first place.
Here is either something I was expressing badly or reading comprehension
issue. I have whole current thread discussed objects managed by smart
pointer, specifically by shared_ptr.
> And yes, there are programs where all objects of some class need to be
> dynamically allocated anyway. Allowing some casual automatic objects in
> the mix would just complicate things like shared_from_this().
That is mostly so but I had impression that we did not discuss it yet.
Seems that I expressed myself not clearly enough.
I was not talking about dynamic/automatic allocation of object with
"additional allocation". I was talking about class that was deliberately
made in a way where make_shared does not work (all the constructors
were made private). The excuse making class in that way was forcing
usage of factory function that was likely made for forcing usage of
shared_ptr.
Therefore separate dynamic allocation for shared state (that I meant
by "additional allocation") was made for each object within that
factory function by constructor of shared_ptr. How else? The
make_shared does not compile. That is insane example for me but YMMV.
Now coming back to what we haven't discussed yet. I agree with you
that typically we want objects of class to be managed in single
fashion to simplify things. There can also be a case where that
"single fashion" is shared_ptr. It makes perfect sense to have
factory function for it.
The whole enable_shared_from_this however is complicated performance
optimization for that case. Apparently author wants to pass around
references to object instead of shared_ptr as performance
optimization. Apparently author needs at the other end of such
reference-passing-chain still have the shared_ptr as complication.
Therefore complicated performance optimization.
Usage of that means like author is not entirely happy about performance.
Where programmer is unhappy about performance there flexibility may help
and there may be desire to relax that "single fashion". Note that I am
not saying that I know what helps just that it may. More efficient smart
pointer or some casual automatic object may help in tightest spot.
So it is only good if calling shared_from_this() of such objects is not
undefined behavior anymore in C++17.