替換失敗並非錯誤

維基百科,自由的百科全書

替換失敗並非錯誤 (Substitution failure is not an error, SFINAE)是指C++語言在模板參數匹配失敗時不認為這是一個編譯錯誤。戴維·范德沃德英語David Vandevoorde最先引入SFINAE縮寫描述相關編程技術。[1]

具體說,當創建一個重載函數的候選集時,某些(或全部)候選函數是用模板實參替換(可能的推導)模板形參的模板實例化結果。如果某個模板的實參替換時失敗,編譯器將在候選集中刪除該模板,而不是當作一個編譯錯誤從而中斷編譯過程,這需要C++語言標準授予如此處理的許可。[2] 如果一個或多個候選保留下來,那麼函數重載的解析就是成功的,函數調用也是良好的。

例子

下屬簡單例子解釋了SFINAE:

struct Test {
  typedef int foo;
};

template <typename T>
void f(typename T::foo) {}  // Definition #1

template <typename T>
void f(T) {}  // Definition #2

int main() {
  f<Test>(10);  // Call #1.
  f<int>(10);   // Call #2. 并无编译错误(即使没有 int::foo)
                // thanks to SFINAE.
}

在限定名字解析時(T::foo)使用非類的數據類型,導致f<int>推導失敗因為int並無嵌套數據類型foo, 但程序仍是良好定義的,因為候選函數集中還有一個有效的函數。

雖然SFINAE最初引入時是用於避免在不相關模板聲明可見時(如通過包含頭文件)產生不良程序。許多程式設計師後來發現這種行為可用於編譯時內省(introspection)。具體說,在模板實例化時允許模板確定模板參數的特定性質。

例如,SFINAE用於確定一個類型是否包含特定typedef:

#include <iostream>

template <typename T>
struct has_typedef_foobar {
  // Types "yes" and "no" are guaranteed to have different sizes,
  // specifically sizeof(yes) == 1 and sizeof(no) == 2.
  typedef char yes[1];
  typedef char no[2];

  template <typename C>
  static yes& test(typename C::foobar*);

  template <typename>
  static no& test(...);

  // If the "sizeof" of the result of calling test<T>(nullptr) is equal to
  // sizeof(yes), the first overload worked and T has a nested type named
  // foobar.
  static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
};

struct foo {
  typedef float foobar;
};

int main() {
  std::cout << std::boolalpha;
  std::cout << has_typedef_foobar<int>::value << std::endl;  // Prints false
  std::cout << has_typedef_foobar<foo>::value << std::endl;  // Prints true
}

當類型T有嵌套類型foobartest的第一個定義被實例化並且空指針常量被作為參數傳入。(結果類型是yes。)如果不能匹配嵌套類型foobar,唯一可用函數是第二個test定義,且表達式的結果類型為no。省略號(ellipsis)不僅用於接收任何類型,它的轉換的優先級是最低的,因而優先匹配第一個定義,這去除了二義性。

C++11的簡化

C++11中,上述代碼可以簡化為:

#include <iostream>
#include <type_traits>

template <typename... Ts>
using void_t = void;

template <typename T, typename = void>
struct has_typedef_foobar : std::false_type {};

template <typename T>
struct has_typedef_foobar<T, void_t<typename T::foobar>> : std::true_type {};

struct foo {
  using foobar = float;
};

int main() {
  std::cout << std::boolalpha;
  std::cout << has_typedef_foobar<int>::value << std::endl;
  std::cout << has_typedef_foobar<foo>::value << std::endl;
}

C++標準的未來版本中Library fundamental v2 (n4562)頁面存檔備份,存於互聯網檔案館)建議把上述代碼改寫為:

#include <iostream>
#include <type_traits>

template <typename T>
using has_typedef_foobar_t = typename T::foobar;

struct foo {
  using foobar = float;
};

int main() {
  std::cout << std::boolalpha;
  std::cout << std::is_detected<has_typedef_foobar_t, int>::value << std::endl;
  std::cout << std::is_detected<has_typedef_foobar_t, foo>::value << std::endl;
}

Boost的使用者在boost::enable_if[3]中使用SFINAE。

參考文獻

  1. ^ Vandevoorde, David; Nicolai M. Josuttis. C++ Templates: The Complete Guide. Addison-Wesley Professional. 2002. ISBN 0-201-73484-2. 
  2. ^ International Organization for Standardization. "ISO/IEC 14882:2003, Programming languages — C++", § 14.8.2.
  3. ^ Boost Enable If. [2020-08-18]. (原始內容存檔於2008-09-05).