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::bind has two forms.
template< class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );
template< class R, class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );
since std::less_equal is struct you should pass to this function as F fully-constructed object. Works code is
#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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With