Why cannot std::stol be converted to a std::function object?
1 2 3 4 5 6 7 8 9 | #include <functional> #include <string> using namespace std; int main() { function<long(const string&, size_t, int)> fn = stol; } |
以上代码无法按预期编译,出现以下错误:
error : no matching constructor for initialization of 'std::function
' (aka 'function , allocator > &, unsigned long long, int)>')
原因有二:
你必须写:
1 2 | function<long(const string&, size_t*, int)> fn = static_cast<long(*)(const string&, size_t*, int)>(stol); |