2012-10-05 67 views
0

我試圖在字符串的矢量上使用boost::trim。據我所知,this solutions做工考究,但是我不明白爲什麼boost :: trim和std :: bind2nd

std::for_each(df.colnames.begin(), df.colnames.end(), 
    std::bind2nd(std::ptr_fun(boost::trim<std::string>), std::locale())); 

不起作用。我收到錯誤:

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在這裏不起作用?

回答

1

有兩個問題,我認爲:

  1. ptr_fun要求其參數返回一個值。參見: http://www.sgi.com/tech/stl/ptr_fun.html

  2. bind2nd不適用於參考參數。請參閱:Using std::bind2nd with references

這個故事的寓意: boost::bind隱藏複雜的一個令人震驚的量。

如果你真的想使它工作,不計較按值傳遞字符串/區域設置,你可以如下包裹裝飾:

int trim2(std::string s, const std::locale loc) 
{ 
    boost::trim<std::string>(s, loc); 
    return 0; 
} 

然後執行:

std::for_each(df.colnames.begin(), df.colnames.end(), 
    std::bind2nd(std::ptr_fun(trim2), std::locale())); 

PS :(1)可能依賴於庫。我只是用g ++試過,並且它沒有void return的問題。