下面的代码编译(并运行)就好了,尽管我希望它产生编译时错误:内部部分专用模板实例++忽略编译时错误
#include <iostream>
using namespace std;
template <typename T>
struct is_int {
static const bool value = false;
};
template<>
struct is_int<int> {
static const bool value = true;
};
// General template definition with default second type parameter (void)
template <typename A, typename B = void>
struct Foo {
static const int i = 42;
};
// Partially specialized template definition with possibly
// undefined second parameter
template<typename A>
struct Foo<A, typename enable_if<is_int<A>::value>::type > {
static const int i = 56;
};
int main() {
cout << Foo<bool>::i << endl; // Outputs '42'
cout << Foo<int>::i << endl; // Outputs '56'
return 0;
}
的enable_if
模板如果第一个参数的值是true
,那么结构Foo
的部分专用化只会定义成员类型type
。请参阅reference page。
因此,当main
函数的第一行实例化模板Foo
时,编译器究竟做了什么?显然,当它试图匹配部分专业化时,它会遇到错误(因为type
未定义)。
它是否简单地放弃这个选择以避免错误?
在C++中没有像_partial模板特化/实例化的东西?!? –
听起来像你在谈论“SFINAE”行为,这是预期的 –
@πάνταῥεῖ:纠正(希望)的标题。 – MightyNicM