【发布时间】:2013-08-31 14:47:46
【问题描述】:
有这种检查类型是否匹配的标准方法:
template<class T, class U>
struct is_same { static const bool value = false; };
template<class T>
struct is_same<T, T> { static const bool value = true; };
我是这样使用它的:
if (SamTypeCheck<double,double>::value)
cout<<"same"<<endl;
else
cout<<"different"<<endl;
if (SamTypeCheck<int,double>::value)
cout<<"same"<<endl;
else
cout<<"different"<<endl;
这在我看来不是线程安全的,因为它使用静态成员变量。它真的不是线程安全的吗?该代码以某种方式使我感到困惑。具有相同功能的线程安全的替代品是什么?
我为什么需要这个?
我有一个用于处理矩阵的模板化类,我想使用 Intel Compiler Math Kernel Library 进行矩阵乘法和求逆,其中每种类型的函数都不同,所以在执行之前我必须知道类型矩阵运算。
谢谢。
【问题讨论】:
-
静态成员不是一个好的设计。使用继承(尽管正如评论中所说,这些都是编译时检查 - 不是线程安全的问题)
标签: c++ multithreading templates static typechecking