thread_local x(2)不调用构造函数

时间:2019-09-03 20:27:29

标签: c++ c++11 ssl thread-local-storage

thread_local在C ++ 11中定义为具有动态初始化语义,因此可以将非POD类型声明为线程本地。但是,在此程序中,我得到了意外的结果:

#include <iostream>
struct A {
  A() : repr_(0) {}
  A(int) : repr_(2) {}
  int repr_;
};
thread_local A x(2);
int main() {
  std::cerr << x.repr_ << "\n";
  return 0;
}

在GCC 4.8上,我得到:

$ g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -std=c++11 test.cpp && ./a.out
0

如果我将thread_local行替换为:

,程序将正常运行。

thread_local A x = A(2);

这是怎么回事?

0 个答案:

没有答案