上級 › error: no matching function for call to '...'(テンプレートの型推論失敗)の原因と直し方 › パターン3
error: no matching function for call to '...'(テンプレートの型推論失敗)の原因と直し方
テンプレート関数の2つの引数に異なる型を渡したときに出るエラーです。
1 #include <iostream> 2 template <typename T> 3 T multiply(T a, T b) { 4 return a * b; 5 } 6 int main() { 7 std::cout << multiply(4, ___) << std::endl; ^ 8 return 0; 9 }
main.cpp:7:26: error: no matching function for call to 'multiply(int, char)'
空欄を埋めてコンパイル・実行を通す
「テンプレートは何でも受け付けてくれる万能な関数」というのは誤解です。'2'はchar型であり、4のint型とは別の型として扱われるため型推論が失敗します。
次の問題