[英]boost::trim and std::bind2nd
I am trying to use boost::trim
on a vector of strings. 我试图在字符串向量上使用
boost::trim
。 I understand that this solutions works elegantly, however I cannot understand why 我知道此解决方案可以优雅地工作,但是我不明白为什么
std::for_each(df.colnames.begin(), df.colnames.end(),
std::bind2nd(std::ptr_fun(boost::trim<std::string>), std::locale()));
doesn't work. 不起作用。 I get error:
我得到错误:
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
Why std::bind2nd
doesn't work here? 为什么
std::bind2nd
在这里不起作用?
There are two problems with this I think: 我认为有两个问题:
ptr_fun
requires that its argument return a value. ptr_fun
要求其参数返回一个值。 See: http://www.sgi.com/tech/stl/ptr_fun.html 请参阅: http : //www.sgi.com/tech/stl/ptr_fun.html
bind2nd
doesn't work with reference arguments. bind2nd
不适用于引用参数。 See: Using std::bind2nd with references 请参阅: 将std :: bind2nd与引用配合使用
Moral of the story: boost::bind
hides a shocking amount of complexity. 故事的寓意:
boost::bind
隐藏了令人震惊的复杂性。
If you really want to make it work and don't care about passing strings/locales by value you can wrap trim as follows: 如果您真的想使其工作并且不关心按值传递字符串/语言环境,则可以按以下方式包装trim:
int trim2(std::string s, const std::locale loc)
{
boost::trim<std::string>(s, loc);
return 0;
}
And then do: 然后执行:
std::for_each(df.colnames.begin(), df.colnames.end(),
std::bind2nd(std::ptr_fun(trim2), std::locale()));
PS: (1) may be library-dependent. PS:(1)可能与库有关。 I just tried with g++ and it didn't have a problem with a void return.
我只是用g ++尝试过,返回空就没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.