1

From dcl.constinit:

No diagnostic is required if no constinit declaration is reachable at the point of the initializing declaration.

What does it mean? I guess an example would be sufficient.

Something dynamically initialized is just ill-formed (from the same link), so it's not that.

deshalder
  • 507
  • 2
  • 13
  • @273K so what's the answer? – deshalder Jun 26 '22 at 20:28
  • 1
    The previous sentence says that a constinit variable must be declared constinit in the initialization. What if it is declared constinit in one file and initialized in another file - without the keyword? "No diagnostic required". – BoP Jun 26 '22 at 20:49
  • 2
    https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1143r2.html See **Applying constinit to declarations or just definitions?** – 273K Jun 26 '22 at 20:53

1 Answers1

2

Suppose you have one translation unit that declares a symbol to be constinit:

// a.cc

#include <iostream>

extern constinit bool has_constinit;

int
main()
{
  std::cout << std::boolalpha << has_constinit << std::endl;
}

Now suppose the translation unit that defined the symbol does not declare it constinit:

// b.cc

#include <cstdlib>

bool has_constinit = std::getenv("CONSTINIT");

You can compile and link these two files together without error, even though it doesn't do what you wanted, because has_constinit is being initialized dynamically.

user3188445
  • 4,062
  • 16
  • 26