2012-08-14 46 views
0

我尝试编译此功能:的std ::绑定的std :: less_equal

#include <algorithm> 
#include <cmath> 
#include <functional> 
#include <vector> 

void foo(unsigned n) 
{ 
    std::vector<unsigned> some_vector; 
    /* fill vector ... */ 
    auto le_sqrt_n = std::bind(std::less_equal<unsigned>, 
          std::placeholders::_1, 
          unsigned(sqrt(n)) 
          ); 
    auto it = std::find_if(some_vector.rbegin(), some_vector.rend(), le_sqrt_n); 
    /* do something with the result ... */ 
} 

用gcc 4.6.3:

g++ -std=c++0x test_bind_02.cc 

,并得到这个错误:

test_bind_02.cc: In function ‘void foo(unsigned int)’: 
test_bind_02.cc:10:55: error: expected primary-expression before ‘,’ token 
test_bind_02.cc:13:9: error: unable to deduce ‘auto’ from ‘<expression error>’ 
test_bind_02.cc:14:77: error: unable to deduce ‘auto’ from ‘<expression error>’ 

我的(可能是愚蠢的)错误是什么?

回答

7

您不要拨打std::less_equal<unsigned>c-tor。 此代码工作正常。

http://liveworkspace.org/code/7f779d3fd6d521e8d4012a4066f2c40f

std::bind有两种形式。

template< class F, class... Args > 
/*unspecified*/ bind(F&& f, Args&&... args); 
template< class R, class F, class... Args > 
/*unspecified*/ bind(F&& f, Args&&... args); 

因为std::less_equal是结构你应该通过这个功能F完全构造的对象。作品代码是

#include <algorithm> 
#include <cmath> 
#include <functional> 
#include <vector> 

void foo(unsigned n) 
{ 
    std::vector<unsigned> some_vector; 
    /* fill vector */ 
    auto le_sqrt_n = std::bind(std::less_equal<unsigned>(), 
       std::placeholders::_1, 
       unsigned(sqrt(n)) 
       ); 
    auto it = std::find_if(some_vector.rbegin(), some_vector.rend(), le_sqrt_n); 
} 
+0

就像那么简单... – steffen 2012-08-14 09:03:26

+0

您是否也可以在此处添加代码?这允许更好的浏览并防止未来的点滴。 – TemplateRex 2012-08-14 09:06:32

相关问题