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

11

u/TheThiefMaster C++latest fanatic (and game dev) Sep 20 '26

It's an optimisation issue. Two functions taking references, one copying and one moving, is the minimal number of operations in each case.

A single function that forces the construction of a temporary at the call site adds to the optimisation load at every call site. Thus: compiles notably slower

7

u/usefulcat Sep 20 '26

Probably also results in more total generated code, due to construction of a temporary at every call site.