2015-02-24 39 views
0

有谁知道,问题在哪里?看看代码片段。模板参数演绎失败,用stoi功能

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

什么是错误讯息? – Xiao 2015-02-24 21:18:06

回答

0

它不会编译的原因是因为“Stoi旅馆”过载:

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