下面的代碼編譯(並運行)就好了,儘管我希望它產生編譯時錯誤:內部部分專用模板實例++忽略編譯時錯誤
#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