###解決!
回答頂いた@EnumHackさん、@rtr1950xさん、ありがとうございました。理解が深まりました。
C++
1#include <cstdio> 2#include <iostream> 3#include <iterator> 4#include <string> 5#include <vector> 6#include <map> 7 8template <typename T1, typename T2> 9bool map_func_key(const std::map<T1, T2> &m, typename std::map<T1, T2>::key_type const &key) 10{ 11 return true; //処理省略 12} 13 14template <typename T1, typename T2> 15bool map_func_value(const std::map<T1, T2> &m, typename std::map<T1, T2>::mapped_type const &value) 16{ 17 return true; //処理省略 18} 19 20int main() 21{ 22 std::map<int, int> map1; 23 std::map<std::string, int> map2; 24 std::map<int, std::string> map3; 25 std::map<std::string, std::string> map4; 26 std::map<std::string, std::vector<std::string>> map5; 27 28 map1[1] = 111; 29 map2["b"] = 222; 30 map3[3] = "ccc"; 31 map4["4"] = "ddd"; 32 map5["5"] = {"555", "eee"}; 33 34 // map_func_key 35 std::cout << map_func_key(map1, 1) << std::endl; 36 std::cout << map_func_key(map2, "b") << std::endl; 37 std::cout << map_func_key(map3, 3) << std::endl; 38 std::cout << map_func_key(map4, "4") << std::endl; 39 std::cout << map_func_key(map5, "5") << std::endl; 40 41 // map_func_value 42 std::cout << map_func_value(map1, 111) << std::endl; 43 std::cout << map_func_value(map2, 222) << std::endl; 44 std::cout << map_func_value(map3, "ccc") << std::endl; 45 std::cout << map_func_value(map4, "ddd") << std::endl; 46 std::cout << map_func_value(map5, {"555", "eee"}) << std::endl; 47 48 getchar(); 49 return 0; 50}
###知りたいこと
template
の使い方でどこが間違っているのか分かりません。
template
を使って型が<std::string,int>
でも<int,int>
でも動作するように汎用的にしたいです。
ご教示よろしくおねがいします。
###現状
参考情報:適当なC++テンプレート入門
template<class T>と記述することでTを任意の型として関数内で使用できます。
リンク先の情報を参考にある関数を汎用的にしようとtemplate
なるものを使ってみました。
主に使う型の組み合わせでテストしてみると<int,int>
では上手くいったのですが、
<std::string,int>
にすると型が違うとのエラーでコンパイルが通りません。
template
なしで<std::string,int>
を直接指定するとエラーは無く出来そうで出来ないので困っています。
####エラー
no instance of function template "map_func1" matches the argument list -- argument types are: (std::map<std::__cxx11::string, int, std::less<std::__cxx11::string>, std::allocator<std::pair<const std::__cxx11::string, int>>>, const char [2]) template<class T1, class T2> bool map_func1(const std::map<T1, T2> &m, T1 key)
####サンプルコード
C++
1#include <cstdio> 2#include <iostream> 3#include <iterator> 4#include <string> 5#include <map> 6 7//template あり 8template <typename T1, typename T2> 9bool map_func1(const std::map<T1, T2> &m, T1 key) 10{ 11 return true;//処理 12} 13 14//template なし 15bool map_func2(const std::map<std::string,int> &m, std::string key) 16{ 17 return true;//処理 18} 19 20int main() 21{ 22 std::map<int,int> map1; 23 std::map<std::string,int> map2; 24 map1[1] = 111; 25 map2["a"] = 222; 26 27 // template あり ->OK 28 // <int,int> 29 std::cout << map_func1(map1,1) << std::endl; 30 31 // template あり ->ここだけNG 32 // <std::string,int> 33 std::cout << map_func1(map2,"a") << std::endl; 34 35 // template なし ->OK 36 // <std::string,int> 37 std::cout << map_func2(map2,"a") << std::endl; 38 39 getchar(); 40 return 0; 41}

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/26 18:48 編集
2018/10/27 13:17 編集
2018/10/27 21:43 編集
2018/10/27 21:38