3

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

PaulH
  • 7,759
  • 8
  • 66
  • 143
  • 1
    Isn't your inner voice telling you that probably you shouldn't be using this stuff? `std::bind` was meant to be a joke, not something to be used for real... – 6502 Feb 01 '15 at 20:18

2 Answers2

3

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
  • 16,391
  • 3
  • 44
  • 59
2

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);
Community
  • 1
  • 1
Barry
  • 286,269
  • 29
  • 621
  • 977