r/cpp • • Sep 20 '26

Magic statics vs. std::call_once

https://devblogs.microsoft.com/oldnewthing/20260916-00/?p=112703
73 Upvotes

11 comments sorted by

View all comments

Show parent comment

-1

u/PossibilityUsual6262 Sep 20 '26

So from my perspective, the code bases i work with, You most likely getting that shared passed 4 call stacks or even more as a const ref, most of them cannot be inlined, from some member variable of some owner system.

I'm looking at it as - your best scenario is you move elision within inlined constructor, and worst scenario is if across some dll boundaries (comes up a lot in my work) you get two copies of shared ptr which ranges from extremely expensive to just expensive, depending if it is thread safe ptr or not.

Like im not seeing what is gained ever if you pass it by value instead of const ref by default. Is the other resources actually say what is the profit? Because to me so far it looks like one more "free abstraction" of modern cpp, and they are not free on average.

Even this one, if moved but not completely inlined, you would get destructor call of shared ptr, so we back to gambling again with "compiler smart enough to see that i moved ptr into variable, control block ref count wasn't ++ so destructor doesn't need to be called".