Examples of constinit declaration not reachable at the point of the initializing declaration

121 views Asked by At

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.

1

There are 1 answers

0
user3188445 On BEST ANSWER

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.