thread_local静态成员模板定义:初始化失败,GCC
thread_local static member template definition: initialisation fails with gcc
当C++类中的静态成员既是thread_local
又是成员模板时,它不会被初始化。
#include <unordered_map>
#include <iostream>
class A {
public:
template<typename T>
thread_local static std::unordered_map<int,T> m;
};
template<typename T>
thread_local std::unordered_map<int,T> A::m{};
int main() {
// A::m<int> = std::unordered_map<int,int>{}; // solves the problem
std::cout << A::m<int>.bucket_count() << std::endl; // returns zero.
A::m<int>.insert({1,2}); // causes SIGPFE (hash modulo bucket_count)
}
unordered_map未初始化,存储桶计数为零。这会导致哈希值取模数时除零。没有thread_local
或没有template
它工作正常。在每个使用它的线程中手动初始化成员(注释行(可以解决问题。
根据C++标准,这是未定义的行为,还是可能是编译器错误?我尝试使用 gcc 7.1.1 和 5.2.0,它们都会产生错误。叮当 3.8 似乎有效。
编辑:我用SVN的gcc 8.0.0 20170817确认了这种行为,并提交了错误报告:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81880
再次,要结束问题:我提交了错误报告,请参阅 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81880
相关文章:
- 是变量初始化失败吗?
- thread_local静态成员模板定义:初始化失败,GCC
- 嵌套列表(字符串向量的向量)初始化失败
- 字符串初始化失败:"expression must have integral or unscoped enum type"
- C++中的即时对象初始化失败,出现LNK2005错误
- GLEW 初始化失败
- OpenCV 中的相机初始化失败
- C++全局对象初始化失败——为什么?以及是否可以将对象放入.DATA部分
- c++静态初始化失败示例
- 对象的c++数组,初始化失败
- C++11 指向同一基类的其他类的指针的非静态数据成员统一初始化失败
- C++ - 矢量初始化失败
- 初始化失败时如何终止qt编程
- 字符串初始化失败
- 变量的静态初始化失败
- 是块作用域静态或线程存储持续时间变量初始化失败的原因
- 继承构造函数+非默认构造函数类型的类内初始化失败
- Direct3d初始化失败/ c++
- 应用程序在Windows xp上初始化失败
- 系统构造函数初始化失败