Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cxx] Remove static initializers. (dotnet#33022)
In a current shipping Visual C++, 19.21.27702.2, there is a bug involving const member data in structs, where for example: static const struct a { // const on this line in either case const int b; // const on this line } c = {1}; will have a static initializer, run usually reliably before main, but: static const struct a { // const on this line in either case int b; // no const on this line } c = {1}; will not. The semantic difference is minuscule, so much so, that the first form is relatively rare, and the bug shipped. Just always use the second form. The difference is that if you have a non-const a, b is still const, I guess. If you have a const a, then no difference. You can find these easily: cd ... for %a in (*.exe *.dll) do link /dump /disasm %a > %a.txt findstr /c:"dynamic initializer" *.txt Which could be automated and tested in CI. This probably not an actual problem, but it is needlessly inefficient, and potentially a problem. The timing/ordering of the initializers is a bit not deterministic. You get writable data where you expect read only. It is in fact a compiler bug, fixed in later releases. But it has been shipping for a while, I first noticed it over two years ago, in code of mine that it caused to not work (a delayload implementation which runs before main). Co-authored-by: Jay Krell <[email protected]>
- Loading branch information