用于检测模板特化的模板元函数

2023-11-23

灵感来自这个问题,我想知道是否可以引入一些编译时检查来检测是否有两个给定的模板实例化:

template <typename T>
class Templ...

typedef Templ<std::string> stringInstance;
typedef Templ<double> doubleInstance;

是根据相同的定义构建的,或者它们是根据不同的专业化构建的Templ模板

所以基本上假设的模板函数的行为如下:

template <typename T>
class Templ
{}

template <>
class Templ<std::string>
{}

template <>
class Templ<double>
{}

template <typename T1,typename T2>
class Belong_To_Same_Templ_Definition
{}

//tests
typedef Templ<std::string> stringInstance;
typedef Templ<double> doubleInstance;
typedef Templ<int> intInstance;
typedef Templ<char> charInstance;

assert( Belong_To_Same_Templ_Definition< intInstance , charInstance >::value == true);
assert( Belong_To_Same_Templ_Definition< intInstance , doubleInstance >::value == false);
assert( Belong_To_Same_Templ_Definition< stringInstance , doubleInstance >::value == false);

可以创建这种元函数吗?


老实说,这似乎不太可能(尽管我不能明确排除这是一个狡猾的把戏)。

对于给定的专业化(在选择它的类型参数之外)没有一流的标识来进行比较。

因此,如果您愿意,您可以使其与您自己的模板一起使用,但您无法为现有模板编写临时推理。

还要考虑到它无论如何都不行,从某种意义上说,它无法判断两个实例化是否具有兼容的布局:即使Templ<int> and Templ<char>从相同的模板代码实例化,没有专门化,该代码可以使用特征类are专门。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用于检测模板特化的模板元函数 的相关文章