用于检测模板专门化的模板元函数
template metafunction for detecting template specialisations
受到这个问题的启发,我想知道是否可以引入一些编译时检查来检测是否两个给定的模板实例化:
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类
相关文章:
- 尝试根据类中 typedef 的存在来专门化模板函数
- 如何基于模板化类的基类专门化成员函数
- 专门化模板覆盖函数/避免对象切片
- 如何使用模板化类专门化模板化函数?
- 线程 std::调用未知类型,无法专门化函数错误
- 如何在编译时专门化大型模板函数中的小部分
- 模板函数,其中一个参数需要专门化,而另一个不需要
- 错误 C2893 无法专门化函数模板'unknown-type std::invoke(_Callable &&,_Types &&...)'
- 从 std 命名空间中专门化函数模板的想法有多糟糕?
- 为模板函数专门化一个模板类
- 基于参数值的c++模板成员函数专门化
- 如何确定函数专门化的主模板
- 等价参数的c++函数专门化
- 基于运行时值调用不同的模板函数专门化
- 跨库的模板函数专门化符号匹配
- 模板函数专门化,c++
- 如何使用type_traits或模板函数专门化来整合模板方法
- 如何在子类中重载模板化函数(专门化)
- 可变模板函数:专门化头/尾和空基准情况
- 不允许void的部分函数专门化-替代解决方案