有谁知道,问题出在哪里?请查看代码段。
template <typename V>
struct Tcn {
template <typename Callable, typename... Args>
Tcn (const string& val, Callable callable, Args... args) {
value = callable (val, args...);
}
V value;
};
int foo (const string& s, size_t* idx = 0, int base = 10) { return {}; }
Tcn<int> tcn1 {"Hello", foo, static_cast<size_t*> (nullptr), 10};
Tcn<int> tcn2 {"Hello", stoi, static_cast<size_t*> (nullptr), 10};
在这里,tcn2会引发问题,但为什么呢? foo和stoi的原型似乎完全相同。
答案 0 :(得分:0)
它没有编译的原因是因为&#39; stoi&#39;超载:
int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );
int stoi( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
所以它不知道选择哪一个。显示这一点的较小示例是:
#include <string>
template <typename Callable>
int fujitsu(Callable callable) {}
int main() {
fujistu(std::stoi);
}
Gcc错误:
prog.cpp:7:22: error: no matching function for call to 'fujitsu(<unresolved overloaded function type>)'
fujitsu(std::stoi);
^
Ideone:http://ideone.com/HnpSC2