Windows 上的 Clang/LLVM 7 和 8 多次初始化内联静态数据成员(使用 link.exe 和 lld-

Clang/LLVM 7 and 8 on Windows initialize inline static data member multiple times (with both link.exe and lld-link.exe)

本文关键字:数据成员 静态 使用 lld- exe link Clang 上的 LLVM Windows 初始化      更新时间:2023-10-16

Clang/LLVM 7 和 8 为每个TU 初始化一次内联静态数据成员。据我了解C++17,这是不正确的。

尽管可以在多个 TU 中定义内联变量,但编译器和/或链接器必须确保它在程序中仅存在一次,因此只初始化一次

下面的小程序显示了Clang/LLVM会发生什么(在Visual Studio 2017和2019 RC中使用LLVM编译器工具链扩展进行了测试(:

标题.h

#include <iostream>
struct A
{
A()      { std::cout << "ctor " << this << std::endl; }
~A()     { std::cout << "dtor " << this << std::endl; }
void f() { std::cout << "f " << this << std::endl;  }
};
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"

此程序打印:

ctor 010D4020
ctor 010D4020
ctor 010D4020
ctor 010D4020
f 010D4020
dtor 010D4020
dtor 010D4020
dtor 010D4020
dtor 010D4020

这是 A 的唯一对象的四个初始化(实际上每个 TU 一个(,而不是一个(如 C++17 要求的那样(。

程序应打印:

ctor 010D4020
f 010D4020
dtor 010D4020

顺便说一下,这就是MSVC所做的。

这是 clang/LLVM 中的一个错误,对吧?

inline关键字的主要功能是它以两种方式修改 ODR 规则:

  1. 允许多个定义(有一些限制(

  2. 生成的对象被"折叠"到单个实例中:

    具有

    外部链接的内联函数或变量在所有翻译单元中应具有相同的地址。

C++17 中唯一的补充是它还允许static数据成员声明成为定义。就是这样。

static数据成员仍然具有相同的链接(在您的情况下是外部链接(、存储持续时间和生存期,并且出于所有实际目的,就像全局定义的变量一样工作。 参见 [class.static.data]/6:

静态数据成员的初始化和销毁与非局部变量完全相同

这基本上意味着它应该与这样工作

struct A
{
A()      { std::cout << "ctor "; }
~A()     { std::cout << "dtor "; }
};
A a; // in one of the TU's
extern A a; // in all other TU's

结论:

这是叮当声中的一个错误。staticS::a必须初始化并销毁一次。

此错误已在基于 SVN r361807 的当前快照构建中修复。