首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost::trim和std::bind2nd

boost::trim和std::bind2nd
EN

Stack Overflow用户
提问于 2012-10-06 06:53:54
回答 1查看 565关注 0票数 0

我正尝试在字符串向量上使用boost::trim。我知道this solutions工作起来很优雅,但是我不明白为什么

代码语言:javascript
复制
std::for_each(df.colnames.begin(), df.colnames.end(),
    std::bind2nd(std::ptr_fun(boost::trim<std::string>), std::locale()));

不起作用。我得到了错误:

代码语言:javascript
复制
error: ‘typename _Operation::result_type std::binder2nd<_Operation>::operator()(typename _Operation::first_argument_type&) const [with _Operation = std::pointer_to_binary_function<std::basic_string<char>&, const std::locale&, void>; typename _Operation::result_type = void; typename _Operation::first_argument_type = std::basic_string<char>&]’ cannot be overloaded

为什么std::bind2nd不能在这里工作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-06 07:41:17

我认为这有两个问题:

  1. ptr_fun要求其参数返回值。请看:http://www.sgi.com/tech/stl/ptr_fun.html
  2. bind2nd不适用于引用参数。请参阅:Using std::bind2nd with references

这个故事的寓意:boost::bind隐藏了令人震惊的复杂性。

如果你真的想让它工作,并且不关心通过值传递字符串/区域设置,你可以像下面这样包装trim:

代码语言:javascript
复制
int trim2(std::string s, const std::locale loc)
{
  boost::trim<std::string>(s, loc);
  return 0;
}

然后执行以下操作:

代码语言:javascript
复制
std::for_each(df.colnames.begin(), df.colnames.end(),
    std::bind2nd(std::ptr_fun(trim2), std::locale()));

附注:(1)可能依赖于库。我刚刚尝试了g++,它没有一个空返回的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12755060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档