【发布时间】:2012-08-22 06:15:49
【问题描述】:
我正在尝试为具有特定名称的内部类的类提供不同的模板特化。我从here 中获得了线索并尝试了以下方法:
#include <iostream>
template< typename T, typename Check = void > struct HasXYZ
{ static const bool value = false; };
template< typename T > struct HasXYZ< T, typename T::XYZ >
{ static const bool value = true; };
struct Foo
{
class XYZ {};
};
struct FooWithTypedef
{
typedef void XYZ;
};
int main()
{
// The following line prints 1, as expected
std::cout << HasXYZ< FooWithTypedef >::value << std::endl;
// The following line prints 0. Why?
std::cout << HasXYZ< Foo >::value << std::endl;
return 0;
}
如您所见,如果我在FooWithTypedef 中测试typedef 定义的类型,它就可以工作。但是,如果类型是真正的内部类,则它不起作用。它也仅在FooWithTypedef 中的typedef-ed 类型与初始模板声明中第二个参数的默认值匹配时才有效(在我的示例中为void)。有人能解释一下这里发生了什么吗?专业化流程在这里如何运作?
【问题讨论】:
-
为什么不简单地使用 enable_if?
-
@ForEveR:我对这东西背后的操作理论很感兴趣。
标签: c++ template-specialization sfinae