-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Description
Bugzilla Link | 37903 |
Resolution | FIXED |
Resolved on | Oct 21, 2019 10:24 |
Version | 8.0 |
OS | Windows NT |
CC | @zmodem,@jyu2-git,@MMory,@rnk,@tstellar |
Extended Description
Clang/LLVM 6.0.0 on Windows initializes an inline static data member once per TU. As far as I understand C++17 this is not correct.
The idea behind initialization of inline variables is that the compiler and/or linker ensure(s) such a variable be initialized exactly once in a program although it may be defined in multiple TUs. Just like with inline functions. See the following little program:
// header.h
#include
struct A
{
A() { std::cout << "ctor "; }
~A() { std::cout << "dtor "; }
void f() { std::cout << "bla "; }
};
struct S
{
inline static A a; // C++17 inline variable, thus also a definition
};
// TU1.cpp
#include "header.h"
int main()
{
S::a.f();
}
// TU2.cpp
include "header.h"
// TU3.cpp
include "header.h"
// TU4.cpp
include "header.h"
This program prints "ctor ctor ctor ctor bla dtor dtor dtor dtor ". That's four objects of A (in fact one per TU) instead of exactly one (as C++17 demands).
It should print "ctor bla dtor ". This is what MSVC does, by the way.
Please also see the discussion on stackoverflow:
https://stackoverflow.com/questions/50982390/clang-llvm-6-0-0-on-windows-initializes-inline-static-data-member-multiple-times