【发布时间】:2016-10-28 15:23:05
【问题描述】:
为了从 strtoxx 调用中整理代码,但仍将它们内联,我想要一个函数模板,例如:
template <typename STR_TO_NUM> static auto StrToNum( const string& s ) {
char* pEnd;
return STR_TO_NUM( s.c_str(), &pEnd, 10 );
}
然后这样称呼它
unsigned long x = StrToNum<strtoul>( "1984" );
但是我收到“模板参数推导/替换失败:”错误。我能做到:
template <typename T, T (*STR_TO_NUM)(const char *, char **, int)> static T StrToNum( const string& s ) {
char* pEnd;
return STR_TO_NUM( s.c_str(), &pEnd, 10 );
}
并在调用时指定返回类型。但感觉那是多余的。有办法避免吗?
我尝试在 C++11 中使用“使用”来“模板 typedef”STR_TO_NUM,但不知道如何为函数类型执行此操作。
谢谢
【问题讨论】:
标签: c++ c++11 templates template-argument-deduction