下面的代码应该只创建一次类thread_local
,但最终每次访问都会对其进行初始化
#include <iostream>
#include <thread>
using std::cout;
using std::endl;
template <typename T>
class Something {
public:
struct TLBookkeeping {
TLBookkeeping() {
std::cout << "TLBookkeeping() " << std::this_thread::get_id() << std::endl;
}
};
static void foo();
static thread_local TLBookkeeping bookkeeping_;
};
template <typename T>
thread_local typename Something<T>::TLBookkeeping Something<T>::bookkeeping_;
template <typename T>
void Something<T>::foo() {
std::cout << &bookkeeping_ << std::endl;
std::cout << &bookkeeping_ << std::endl;
}
namespace {
struct Struct {};
}
int main() {
Something<Struct>::foo();
}
(https://wandbox.org/permlink/fgqCDHV0axKDRt89)
有趣的是,该错误仅在Struct
位于匿名名称空间中时显示。
有人知道这是Clang错误(gcc正确吗-https://wandbox.org/permlink/hsxRj8OdYbt4Eeck)还是thread_local
的固有错误用法?如果是后者,正确的用法是什么?如果是前者,错误到底是什么?并记录在某处吗?我们应该打开错误报告吗?
答案 0 :(得分:1)
当您发现编译器导致相对确定的运行时行为时,这是不正确的;而另一个流行的编译器不会导致相同的行为-IMHO就足以对上述编译器提交错误。
根据@SamVarshavchik的建议,您:
我已经做到了,所以现在有了bug 42111。
在最坏的情况下-它会被标记为骗子。
编辑:这不是一个骗子,并且刚刚(2019年6月5日)已修复。