3 ms·
The idiomatic version has gotten faster since C++03 overall thanks to move semantics. std::unique_ptr<Foo> doesn't have any overhead over Foo* in code where se
by int_19h 4y ago
The idiomatic version has gotten faster since C++03 overall thanks to move semantics.
std::unique_ptr<Foo> doesn't have any overhead over Foo* in code where semantics is the same (i.e. you want to delete when going out of scope) on any sensible ABI - it has the same storage size, and all operations are trivially inlineable to the same exact thing you'd do with a raw pointer. About the only time I can think of where it can be slower is when the ABI insists that a struct cannot be passed or returned in a register.
- kccqzy 4y ago> std::unique_ptr<Foo> doesn't have any overhead over Foo* in code where semantics is the same Not true. And you just refuted yourself by being up the ABI question. You should watch Titus Winters' presentation on this topic where he compiles code using unique_ptr and raw pointers, compares the assembler produced, and explains why unique_ptr has non-zero overhead. The overhead is indeed coming from the ABI. Google has been wanting the next C++ to break ABI but they didn't get enough votes in the standard committee. https://youtu.be/rHIkrotSwcc https://youtu.be/rHIkrotSwcc
- ncmncm 4y agoNot breaking binary compatibility is a pure good. The standard could pull unique_ptr in as a built-in feature with a different name, and retain backward compatibility at cost of an unfortunate redundancy. Code using unique_ptr would gradually transition to the new thing. Of course each would have a move constructor from the other, to ease the transition.