Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unresolved overloaded function error when binding to std::operator== [duplicate]

Tags:

c++

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

like image 484
PaulH Avatar asked Jun 03 '26 07:06

PaulH


2 Answers

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);
like image 165
Pradhan Avatar answered Jun 05 '26 00:06

Pradhan


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);
like image 31
Barry Avatar answered Jun 05 '26 02:06

Barry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!