I am trying to bind to bool std::operator==(const std::string&, const std::string&), but I'm getting an error that I hope somebody can help me with.
std::string v1 = "foo";
std::string v2 = "foo";
bool r =
std::bind(
static_cast<bool(*)(const std::string&, const std::string&)>(&std::operator== ),
std::placeholders::_1,
std::cref(v1))(v2);
error: invalid static_cast from type '<unresolved overloaded function type>' to type 'bool (*)(const string&, const string&) {aka bool (*)(const std::basic_string<char>&, const std::basic_string<char>&)}'
&std::operator== ),
^
example: ideone.com
Disambiguating std::operator== solves your problem, but it isn't the cleanest looking code:
bool r =
std::bind(
static_cast<bool(*)(const std::string&, const std::string&)>(operator==<char, std::string::traits_type, std::string::allocator_type>),
std::placeholders::_1,
std::cref(v1))(v2);
Pradhan gives you the correct syntax for using bind in this spot. Which seems like a great reason to not use bind in this spot and to prefer a lambda:
bool r = [&v1](const std::string& rhs){
return rhs == v1;
}(v2);
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