C++ 从指向成员模板函数的指针转换为函数类型对象
我已经有了这个代码:C++ 从指向成员模板函数的指针转换为函数类型对象,c++,c++11,C++,C++11,我已经有了这个代码: // mem_fun example #include <iostream> #include <functional> #include <vector> #include <algorithm> #include <string> #include <sstream> #include <map> using namespace std; struct C { C(int i)
// mem_fun example
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <map>
using namespace std;
struct C
{
C(int i): y_(i) {};
int y_;
string op1(int x)
{
std::ostringstream oss;
oss << "operation 1: " << x+y_;
return oss.str();
}
string op2(string x)
{
std::ostringstream oss;
oss << "operation 2: " << x << "+" << y_;
return oss.str();
}
};
struct container: map<string, C>
{
// doesn't compile
// void safeOperation(string key, ??? bound_function_and_arg_object )
template< typename argType >
void safeOperation(string key, string (C::*mf)(argType a), argType a)
{
iterator it = find(key);
if (it != end())
{
C* pC = &(it->second);
cout << (pC->*mf)(a) << "\n";
}
else
{
cout << "key: " << key << " missing\n";
}
}
};
int main () {
container objects;
objects.insert(container::value_type("a1", C(1)));
objects.insert(container::value_type("b2", C(2)));
objects.insert(container::value_type("c3", C(3)));
objects.safeOperation("a1", &C::op1, 1);
objects.safeOperation("b2", &C::op1, 2);
objects.safeOperation("d4", &C::op1, 4);
objects.safeOperation("c3", &C::op2, string("3"));
return 0;
}
示例代码如下:
我可能错过了一些简单的东西,但是谢谢你的帮助
boost/std::bind
创建具有特定于实现类型的对象。唯一的要求是可以使用操作符()
调用对象
要处理任何函数对象,可以通过以下方式更改函数模板:
template< typename F >
void safeOperation(string key, F f)
{
// ...
cout << f(pC) << "\n";
// ...
objects.safeOperation("someKey", bind(&C::op1, placeholders::_1, 4));
模板
无效安全操作(字符串键,F)
{
// ...
cout当您绑定成员函数时,第一个参数必须是其成员函数所在类的实例。因此,您要做的是泛化安全操作
,以获取可在C*
上调用的任何函数:
template< typename F >
void safeOperation(string key, F func) {
iterator it = find(key);
if (it != end())
{
C* pC = &(it->second);
cout << func(pC) << "\n";
}
else
{
cout << "key: " << key << " missing\n";
}
}
好的,这很有效,也很有意义,但我有点惊讶,没有一种特定于成员函数的绑定形式使用占位符,因为不断重复占位符似乎很乏味……@BitBlitz你可以写一个。这是一个非常特殊的用例,只想绑定参数,而不想绑定成员函数的实例,而且它只是一个几行就可以了。我试着写了一行。我想出了一个可以在GCC 4.6编译器上运行的东西,但不能在CPP.SH上运行。上面问题的详细信息。@BitBlitz不要将你的问题编辑为不同的问题。改为问一个新问题。
template< typename F >
void safeOperation(string key, F func) {
iterator it = find(key);
if (it != end())
{
C* pC = &(it->second);
cout << func(pC) << "\n";
}
else
{
cout << "key: " << key << " missing\n";
}
}
using namespace std:;placeholders;
objects.safeOperation("a1", std::bind(&C::op1, _1, 1));
// ^^
// placeholder for pC