c++ - std::string 的函数指针的 typedef 不起作用

标签 c++ function-pointers boost-bind

我正在尝试定义一个特定的函数指针类型,以便在我的 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

和绑定(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

关于c++ - std::string 的函数指针的 typedef 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12156686/

相关文章:

c++ - 绑定(bind)绑定(bind)函数作为参数

c++ - 如何计算二维数组中的重复项数? (C++)

c++ - 尝试解决段错误

c++ - 在全局和局部范围内定义和分配指向函数的指针

c++ - 在 C 代码中使用 boost::bind() 会起作用吗?

c++ - 一般来说,boost bind 在幕后是如何工作的?

c++ - 为什么此代码返回段错误错误?

c++ - 错误 C2679 : binary '>' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

C++通过字符串名称调用不同的函数

c - C 中的函数指针是如何工作的?