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