文字列を格納するコンテナを一気にdoubleに変換するプログラムでちょっと嵌った
#include <string> #include <vector> #include <algorithm> using namespace std; double stod(const std::string& s) { return atof(s.c_str()); } double stod(const std::wstring& s) { return _wtof(s.c_str()); } int main() { string ar[] = {"3.14", "1.23", "5.67"}; vector<double> v; transform(ar, ar + 3, back_inserter(v), &stod); // エラー!あいまいな関数を指定した return 0; }
ありゃ・・・
こうかな、と
transform(ar, ar + 3, back_inserter(v), static_cast<double (*)(const string&)>(&stod)); // OK
うーむ・・・
オーバーロードされた関数を渡すには、どのオーバーロードか明確にするためにキャストしなきゃいけないのか
※_wtofはVC++にあるwchar_tの文字列をdoubleに変換する関数