在 Boost 1.54.0 中,模板函数定义为:
template<class Ch>
std::basic_string<Ch> trim(const std::basic_string<Ch> &s,
const std::locale &loc = std::locale())
{
...
}
在 Boost 1.57.0 中,模板函数更新为:
template<class Str>
Str trim(const Str &s, const std::locale &loc = std::locale())
{
...
}
我为 boost 1.54.0 工作的函数调用是:
void read_command_line(int argc,char** argv,...){
string text = boost::property_tree::detail::trim<char>(argv[i]);
}
现在切换到 Boot 1.57.0 后,我的实现的错误消息是:
error: no matching function for call to 'trim(char*&)
我看到参数变量在两个版本中都被引用为“&s”,为什么会出现上述错误?你能帮助我如何更新我的代码并解释一下吗?
最佳答案
这是一个模板函数,boost 开发人员似乎已经预料到模板参数将从函数参数中推断出来。通过显式提供参数,您打破了这个假设。
通过参数提供类型:
auto text = boost::property_tree::detail::trim(string{argv[i]});
这应该与新旧界面兼容。
新接口(interface)的优点是它支持所有类字符串的类,而不仅仅是那些派生自 std::basic_string
的类。
关于c++ - boost 库更新后模板函数调用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36199981/