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);
这是怎么回事?