我正在尝试定义在我的boost::bind调用中使用的特定函数指针类型,以解决与无法识别的函数重载相关的问题(通过调用static_cast)。我正在明确定义该typedef来解决std::string::compare上的歧义。

当我编写此函数时,出现错误。

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


您知道这个typedef有什么问题吗?

最佳答案

我想你想要这个。

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


例。

#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

和绑定的例子

#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

07-24 09:45
查看更多