【发布时间】:2013-07-01 20:01:05
【问题描述】:
考虑一种情况,需要在另一个模板的虚拟参数中使用另一个模板 g(例如,可能是一些 enable_if 表达式)验证类型 T,如下所示:
template<class> struct g { typedef void type; };
template<class, class> struct f {};
template<class T> struct f<T, void> {}; // Case A
template<class T> struct f<T*, typename g<T>::type> {}; // Case B
int main() { f<int*, void> test; }
这里,为了简单起见,g 并没有真正做任何事情。 Case B 中的第二个参数是在一个非推导的上下文中,因此直觉上人们会认为 Case B 比 Case A 更专业。遗憾的是,gcc 和 clang 都会抱怨模板在上面的实例化中不明确。
如果要删除虚拟参数,那么它编译得很好。添加非推导参数如何以某种方式破坏T* 比T 更专业的合理预期?
下面是使用替换算法的快速检查:
f<Q , void >
-> f<T*, g<Q>::type> // [failed]
f<Q*, g<Q>::type>
-> f<T , void > // [to fail or not to fail?]
// One would assume that 2nd parameter is ignored, but guess not?
【问题讨论】:
标签: c++ template-specialization template-meta-programming sfinae