用于检测模板专业化的模板元功能 [英] template metafunction for detecting template specialisations

查看:59
本文介绍了用于检测模板专业化的模板元功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Inspired by this question, i'm wondering if there is some compile-time check one can introduce to detect if two given template instantiations:

template <typename T>
class Templ...

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

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

are constructed from the same definition, or if they are built from different specializations of the Templ template

因此,基本上,假设的模板函数的行为如下:

so basically the hypothetical template function will behave like this:

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);

可以创建这种元功能吗?

is possible to create this kind of metafunction?

推荐答案

说实话,这似乎不太可能(尽管我不能确定地排除狡猾的把戏).

It seems unlikely, to be honest (although I can't definitively rule out a cunning trick).

给定的专业化(在选择它的类型参数之外)没有一流的身份可以进行比较.

There is no first-class identity for a given specialization (outside the type arguments that select it), to compare.

因此,您可以根据需要使它与您自己的模板一起使用,但不能为现有模板编写临时推断.

So, you could make it work with your own templates, if you want, but you can't write an ad-hoc inference for existing templates.

从某种意义上说,它无法分辨两个实例是否具有兼容的布局:即使 Templ< int> Templ< char> 是从同一模板代码实例化的,没有特殊化,该代码可以使用 特殊化的特征类.

Consider also that it wouldn't work anyway, in the sense that it couldn't tell whether two instantiations have a compatible layout: even if Templ<int> and Templ<char> are instantiated from the same template code, with no specialization, that code can use traits classes which are specialied.

这篇关于用于检测模板专业化的模板元功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆