3 ms·
> Even std::unique_ptr has runtime costs in release builds that a raw pointer does not. O RLY ? https://gcc.godbolt.org/z/QhbUjI https://gcc.godbolt.org/z/Qhb
by jcelerier 7y ago
> Even std::unique_ptr has runtime costs in release builds that a raw pointer does not.
O RLY ? https://gcc.godbolt.org/z/QhbUjI https://gcc.godbolt.org/z/QhbUjI
- abainbridge 7y agoHow would I create the pointer to int in the unique_ptr case without initializing the int? I tried this: https://gcc.godbolt.org/z/SfVxzU https://gcc.godbolt.org/z/SfVxzU I expect there is a way, just I don't know it.
- jcelerier 7y agoSee the docs : https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique https://en.cppreference.com/w/cpp/memory/unique_ptr/make_uni... For C++ < 20 replace `std::make_unique<int>()` by `std::unique_ptr<int>(new int)` (which with all explicit uses of `new` can end up not being safe in some cases (in that case, only if you've not enough memory to allocate an int which should not really be a common occurence...) For C++ >= 20 you get std::make_unique_default_init which does that properly.
- deleted 7y ago[deleted]
- einpoklum 7y agoHave created a Stackoverflow question focused on this point: https://stackoverflow.com/q/58050872/1593077 https://stackoverflow.com/q/58050872/1593077
- Pombal 7y agoYes, really. It's fine for simple types but for more complex types in more complex situations, you pay the price. Unique pointers carry around not just the type but also a default deleter, if you provide one. That deleter has to be copied around, checked for nullptr before execution and set to nullptr when ownership changes. For even more examples of this have a look at this talk when it comes out: https://cppcon2019.sched.com/event/Sfq4/there-are-no-zero-cost-abstractions https://cppcon2019.sched.com/event/Sfq4/there-are-no-zero-co...
- rcxdude 7y agoOnly if the deleter you define has any state, which is very rare and in that case you would need to copy that data around anyway... (for example: https://gcc.godbolt.org/z/mU7hub https://gcc.godbolt.org/z/mU7hub)
- pingyong 7y agoIt's pretty stupid to compare a unique_ptr with a deleter that contains state to a pointer - obviously those two things are completely different and the unique_ptr contains way more information.