用于检测模板专门化的模板元函数

template metafunction for detecting template specialisations

本文关键字:函数 专门化 用于 检测      更新时间:2023-10-16

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

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>Templ<char>是从相同的模板代码实例化的,没有专门化,该代码可以使用专门化的trait类