Search code examples
c++function-pointersboost-bind

typedef of a function pointer for std::string doesn't work


I am trying to defining a specific function pointer type to be used in my calls of boost::bind to resolve issue related to function overloaded not recognized ( by calling static_cast ). I am defining that typedef explicitly for resolving ambiguity on std::string::compare.

When I write this function I got errors.

   typedef int(std::string* resolve_type)(const char*)const;

Do you know what's wrong with this typedef?


Solution

  • I think you want this.

    typedef int(std::string::*resolve_type)(const char*) const;
    

    Example.

    #include <iostream>
    #include <functional>
    
    typedef int(std::string::*resolve_type)(const char*)const;
    
    int main()
    {
       resolve_type resolver = &std::string::compare;
       std::string s = "hello";
       std::cout << (s.*resolver)("hello") << std::endl;
    }
    

    http://liveworkspace.org/code/4971076ed8ee19f2fdcabfc04f4883f8

    And example with bind

    #include <iostream>
    #include <functional>
    
    typedef int(std::string::*resolve_type)(const char*)const;
    
    int main()
    {
       resolve_type resolver = &std::string::compare;
       std::string s = "hello";
       auto f = std::bind(resolver, s, std::placeholders::_1);
       std::cout << f("hello") << std::endl;
    }
    

    http://liveworkspace.org/code/ff1168db42ff5b45042a0675d59769c0