4 ms·
It seems more robust to just make std::make_*() a friend function than to pepper the code with news.
by deadbeeves 4y ago
It seems more robust to just make std::make_*() a friend function than to pepper the code with news.
- planede 4y agoI agree. If you can, just use the make_* functions. It also reduces needless repetition. For `make_*<Widget>(args...)` I need to write the type once, for `*_ptr<Widget>(new Widget)` I need to write the same type twice. Not counting cases for `Base/Derived`.
- MauranKilom 4y agoThat only works if `std::make_*` is actually where `new` is called. It could just as well be called deeper in some library implementation detail like `std::__gnu_cxx_v3::__construct_as(...)` (made up example), in which case you're SOL.
- deadbeeves 4y agoHmm... Good point. I don't think it's a portable solution.
- account42 4y agoIt also would allow anyone to construct your class via make_* which kind of defeats the point of making the constructor private in the first place.