I try to compile this function:
#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 ... */
}
with gcc 4.6.3:
g++ -std=c++0x test_bind_02.cc
and get this error:
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>’
What is my (presumably stupid) mistake?
You don’t call
std::less_equal<unsigned>c-tor.This code works okay.
http://liveworkspace.org/code/7f779d3fd6d521e8d4012a4066f2c40f
std::bindhas two forms.since
std::less_equalis struct you should pass to this function asFfully-constructed object. Works code is