CppCon 2018: Greg Falcon “Initialization, Shutdown, and constexpr”
Vložit
- čas přidán 28. 05. 2024
- CppCon.org
-
Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/CppCon/CppCon2018
-
It's easy to create an access a global object in C++, but doing so correctly and safely is unfortunately a bit tricky. But why?
This talk will take a close look at the various rules that govern when an object is safe to access, including storage duration, object lifetime, initialization, and program termination. We'll look at some safe idioms for creating global singletons that are justified by these rules. And we'll look at the relevant, helpful features added in various revisions of C++, such as constexpr constructors, and how they make the situation better.
-
Greg Falcon
Staff Software Engineer, Google
Greg Falcon is a Staff Software Engineer at Google, working on the Abseil C++ library.
-
Videos Filmed & Edited by Bash Films: www.BashFilms.com
*-----*
Register Now For CppCon 2022: cppcon.org/registration/
*-----*
"A null pointer-to-data-member has internal representation -1" was a revelation
Really interesting!
Many things I hadn't thought about before (and I'm only a little way in), thanks!
Fantastic
At 17:12,
constexpr int Square(int v) { return v * v; }
int one_hundred = Square(10);
You said that initialization of the variable is guaranteed to be at compile time. Was it because the variable has static duration?
Because, for automatic duration variable, standard doesn't guarantee that.
Yes, good point. This talk is almost entirely about objects with static storage duration. I mention this at around 7:00 but I could have emphasized this more.
At one point I had more `namespace`s on slides to make the storage duration clearer, but I took many out for space reasons. This is very much "slide code".
No, it's because of the constexpr keyword _and_ the fact that Square() was called with a constant.
Good, pure useful information!
Glad to hear that!
Why are initializers using parenthesis. Why no brace-initializer?
At czcams.com/video/6ZOygaUjzjQ/video.html , there's a typo for Trivial Default Construction. It should be "Its default constructor is *not* user-defined"
third option: reading in a configuration file after main starts
I happen to like DSO Destruction order, better than calling std::at_exit. I work mostly on POSIX which mandates DSO Destructor order.
@12:46... Beware, PERFORMANCE WARNING!!! Since C++11, function-scope static variables are protected by a hidden synchronization object (a mutex), it will be locked not only on first initialization of the local static variable, but on every single call to the function. This does not happen with variables in the global scope.
If there is atomic flag to mark initialization as complete there is no need to lock a mutex in subsequent calls. Just check the flag and if set - assume that local static has been initialized. Only the first call might need a mutex/condition variable so that if one thread performs the very first call it marks the object as "initialization in progress" and the other threads which happen to call the function at that moment have to wait until the flag is set to "initialization complete".
The true way to zero-init things is double curly braces.