SFINAE: Substitution Failure Is Not An Error (替換失敗不是錯誤)
SFINAE 可用于模板類型參數(shù) 和 返回值類型 兩個地方,
在這兩個地方做類型不正確的操作 它不會報錯和退出程序, 而是跳過當前匹配去嘗試其他匹配.
?
函數(shù)體內(nèi)做類型不正確的操作, 會報錯.
#include <iostream>
using std::cout;
using std::endl;
struct book {
// 重新定義一個類型, 這個類型叫做 page_type
// page_type 屬于 book 作用域中的一個成員.
typedef int page_type;
// 使用另外一種語法來重新定義一個類型, 這個類型叫做 name_type,
// name_type 屬于 book 作用域中的一個成員.
using name_type = string;
};
int main(void) {
// 使用book結(jié)構(gòu)中的類型別名來定義變量.
book::page_type a = 1099;
book::name_type b = "C++ Standard Library";
// author_type 不存在于 book 結(jié)構(gòu)中, 因此會報錯.
book::author_type c = "Nicolai M.Josuttis"; // error.
// output:
// 'author_type': is not a member of 'book'
return 0;
}
?
模板尖括號里做類型不正確的操作, 不會報錯.
#include <iostream>
using std::cout;
using std::endl;
struct book {
typedef int page_type;
using name_type = string;
};
// T::author_type 是一個不存在的東西, 在這里叫做替換失敗, 不是類型操作錯誤.
// 當替換失敗時, 編譯器會跳過這個匹配, 繼續(xù)尋找下一個模板函數(shù).
template<typename T, class U = typename T::author_type>
void example(T t) {
cout << "template<typename T = book::author_type > void example();" << endl;
}
template<typename T>
void example(T t) {
cout << "void example(book b)" << endl;
}
int main(void) {
book b;
example(b);
return 0;
}