lur*_*her 8 c++ static-assert compile-time template-specialization template-meta-programming
受这个问题的启发,我想知道是否有一些编译时检查可以引入检测两个给定的模板实例:
template <typename T>
class Templ...
typedef Templ<std::string> stringInstance;
typedef Templ<double> doubleInstance;
Run Code Online (Sandbox Code Playgroud)
是从相同的定义构造的,或者如果它们是从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);
Run Code Online (Sandbox Code Playgroud)
有可能创造这种元功能吗?
老实说,这似乎不太可能(尽管我不能明确排除这是一个狡猾的把戏)。
对于给定的专业化(在选择它的类型参数之外)没有一流的标识来进行比较。
因此,如果您愿意,您可以使其与您自己的模板一起使用,但您无法为现有模板编写临时推理。
还要考虑到它无论如何都不起作用,因为它无法判断两个实例化是否具有兼容的布局:即使 和Templ<int>是Templ<char>从相同的模板代码实例化的,没有专门化,该代码可以使用以下特征类:专门的。