CppCon 2018: Greg Falcon “Initialization, Shutdown, and constexpr”

preview_player
Показать описание


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.


*-----*
*-----*
Рекомендации по теме
Комментарии
Автор

"A null pointer-to-data-member has internal representation -1" was a revelation

oktal
Автор

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.

dineshkhandelwal
Автор

Really interesting!
Many things I hadn't thought about before (and I'm only a little way in), thanks!

davidledger
Автор

@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.

tikabass
Автор

third option: reading in a configuration file after main starts

zhaoli
Автор

Why are initializers using parenthesis. Why no brace-initializer?

connorhorman
Автор

I happen to like DSO Destruction order, better than calling std::at_exit. I work mostly on POSIX which mandates DSO Destructor order.

connorhorman
Автор

The true way to zero-init things is double curly braces.

connorhorman