【发布时间】:2012-12-18 03:16:12
【问题描述】:
除了预处理器,我如何有条件地启用/禁用显式模板实例化?
考虑:
template <typename T> struct TheTemplate{ /* blah */ };
template struct TheTemplate<Type1>;
template struct TheTemplate<Type2>;
template struct TheTemplate<Type3>;
template struct TheTemplate<Type4>;
在某些编译条件下,Type3与Type1相同,Type4与Type2相同。发生这种情况时,我得到一个错误。我想检测类型是相同的,而不是像在中那样在 Type3 和 Type4 上实例化
// this does not work
template struct TheTemplate<Type1>;
template struct TheTemplate<Type2>;
template struct TheTemplate<enable_if<!is_same<Type1, Type3>::value, Type3>::type>;
template struct TheTemplate<enable_if<!is_same<Type2, Type4>::value, Type4>::type>;
我已经转移了自己的注意力来尝试 enable_if 和 SFINAE(我相信我知道它们为什么会失败),但只有预处理器起作用了(呃)。我正在考虑将类型放入元组或可变参数中,删除重复项,然后将剩余部分用于实例化。
有没有办法根据模板参数类型有条件地启用/禁用显式模板实例化?
【问题讨论】:
-
显式实例化一个类是否意味着隐式实例化所有基类型?
-
@jogojapan - 不,这个明确的 instantiation 不同于 specialization。
-
其实为什么重复显式实例化会出错?
-
@BenVoigt 这是一个错误,原因与函数的多个定义是错误的原因相同:ODR 违规。显式实例化不是模板(具有弱链接),它是一个类,因此只能定义一次。