(本题与C++11/C++14无关:示例使用C++03编译)
enable_bool<T>
::type
只有当T
是时才有成员bool
template <class T>
struct enable_bool
{};
template <>
struct enable_bool< bool >
{ typedef bool type; };
在下一个片段中,部分特化是正确的(参见gcc.godbolt.org)
template <class T, class U, class Enable = T>
struct Foo
{
static int bar() { return 0; }
};
template <class T, class U>
struct Foo< T, U, typename enable_bool<T>::type >
{
static int bar() { return 1; }
};
int main()
{
return Foo <int, bool>::bar();
}
由于enable_bool<T>::type
已经对应于T
(when T
is bool
)
,我们很想分解参数T
和Enable
。
但编译器抱怨(见gcc.godbolt.org)
template <class T, class U>
struct Foo
{
static int bar() { return 0; }
};
template <class T, class U> //ERROR non-deducible template parameter 'T'
struct Foo< typename enable_bool<T>::type, U >
{
static int bar() { return 1; }
};
为什么编译器不能T
在上述部分特化中推导出模板参数?