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

Sdílet
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/
    *-----*

Komentáře • 17

  • @oktal3700
    @oktal3700 Před 5 lety +9

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

  • @davidledger5941
    @davidledger5941 Před 5 lety

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

  • @yuehuajia3176
    @yuehuajia3176 Před 5 lety

    Fantastic

  • @dineshkhandelwal165
    @dineshkhandelwal165 Před 5 lety +2

    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.

    • @gregfalcon8664
      @gregfalcon8664 Před 5 lety +3

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

    • @tikabass
      @tikabass Před 5 lety

      No, it's because of the constexpr keyword _and_ the fact that Square() was called with a constant.

  • @siwenzhang
    @siwenzhang Před 3 lety

    Good, pure useful information!

    • @CppCon
      @CppCon  Před 3 lety

      Glad to hear that!

  • @connorhorman
    @connorhorman Před 5 lety

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

  • @etorrie
    @etorrie Před 5 lety

    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"

  • @zhaoli2984
    @zhaoli2984 Před 5 lety

    third option: reading in a configuration file after main starts

  • @connorhorman
    @connorhorman Před 5 lety

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

  • @tikabass
    @tikabass Před 5 lety

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

    • @pazdziochowaty
      @pazdziochowaty Před 5 lety

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

  • @connorhorman
    @connorhorman Před 5 lety

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