C++ Weekly - Ep 382 - The Static Initialization Order Fiasco and C++20's constinit

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

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

This is cool. I didn't know that CE had a project mode. Can you share projects that way? It'd certainly be a neat way to share larger examples.

anon_y_mousse
Автор

I believe it is worth to mention that the initialization of inline variables (C++17) are partially ordered, they can depend on each other which makes them viable to implement global services (e.g. allocators, debug helpers).

luppy
Автор

I mean it's nice to have that fixed, but static globals are code smell anyway.

TsvetanDimitrov
Автор

Is there a way to request constinit'ness in consumer?
Like if provider was in a third party library and I need to know if I can use this library objects safely, e.g. in code executed when my shared library is loaded?

andreyk.
Автор

Is the guarantee here that constinit does not modify compiler behavior in any way (i.e. removing them at the end in this case was OK) ? IOW it always serves to *validate* compiler behavior for the case when you have constexpr constructors.

userrand
Автор

Interesting portable way especially if it resolves heirarchical dependencies. I have been using the constructor attribute for years to control global construction/init sequencing in the embedded world, but this is certainly not portable, but it doesn't require the class constructor to be constexpr.

markspieth
Автор

I'm glad I've never had to worry about this sort of circumstance..
Also, I would say you could easily have gone with a small font size in CE. Would make it a little easier to read the cramped windows.

metal
Автор

What if there are multiple statics which need to be static initialized with constinit ?what will be the order then ?

yb
Автор

There is actually a well defined way to ensure the order they are initialized in, define them within the same TU. (e.g. make a globals.cpp)

reductor_
Автор

Nice Video. And that CE mode seems useful

aibou
Автор

Are you saying "constinint" or are my ears broken?

homomorphic
Автор

We can put such objects in one struct and make that struct object to be global. This guaranties the initialization order of members.

dimon_ksi
Автор

Seems like this is only useful in aesotheric and fictional code:
It works if Global A depends on Global B, and B can be constexpr constructed.

If there are more than 2 globals involved? Supposedly magic happens.
If any of the depended-on globals does not have a constexpr-constructor? Doesn't work.

"constinit" seems to try to work on too many problems and fails to plug many of the holes. It really only should be used for marking variables that need to be const-initialised... and even at that it is not good cause that can and will still happen at runtime - the only guarantee the language really makes is that it it evaluated before all dynamic initialisation - aka before main is called - but it says absolutely nothing about whether it is done at compiletime or not (just most compilers try to do it at compiletime when they can resolve it)

ABaumstumpf
Автор

Still feel that putting global in function is superior than constinit, but it's nice to have alternative approach

surrog
Автор

I know about Kongsberg in Norway, but never knew there was a Kongsberg in Sweden as well

baardi
Автор

How can we link to gtest in this view ? How to update CMake ?

rafalmichalski
Автор

How to add new files in this CMake view ?

rafalmichalski
Автор

That's why I always use singleton pattern in case I ever need a global object at all, so it is constructed on the first call and not before main. If I need early init I just can call it from somewhere early in the program. Of course this also means additional mutex and/or atomic access, but everything has a price and global objects should be avoided anyway.

necro_ware
Автор

Not sure if anyone's mentioned it, but your audios volume is low in this and other videos of yours :)

mworld
Автор

I will also hate anybody that overloads &. It's just so unexpected even if you know about the possibilities...

dagoberttrump