c++ - boost 库更新后模板函数调用不起作用

标签 c++ templates boost

在 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/

相关文章:

c++ - 如何从模板函数返回模板类?

javascript - 如何在 ng-template 中使用响应式表单

c++ - 如何在 boost.interprocess 共享内存中使用复杂结构的 "push_back"函数 vector

c++ - 用于创建混合类的 MPL 工厂方法

c++ - 不使用 for/while 循环的 Pascal 三角形计算

c++ - 最小可能的分母/除数是多少?

html - 在具有空格的对象的 django 模板中创建超链接

c++ - 在 VS2010 中使用 list_of 调用不明确

c++ - 约束成员模板的外定义规则是什么?

c++ - 使用 boost::lambda_ 压缩字符串中的空格