mem_fn 成员对象的函数
mem_fn to function of member object
我正在修补 std::mem_fn
,但无法将其绑定到结构成员的 data/functions(更深一层)。
我希望代码能比我描述的更好地说明问题,因为我不熟悉术语。
#include <functional>
struct Int
{
Int(int _x = 0) : x(_x) {}
int GetInt() const { return x; }
int x;
};
struct IntWrapper
{
IntWrapper(int _x = 0) : test(_x) {}
int GetWrappedInt() const { return test.GetInt(); }
Int test;
};
int main()
{
IntWrapper wrapper{ 123 };
auto x = std::mem_fn(&IntWrapper::GetWrappedInt);
//auto y = std::mem_fn(&IntWrapper::test.GetInt); // ERROR
//auto z = std::mem_fn(&IntWrapper::test.x); // ERROR
int a = x(wrapper);
//int b = y(wrapper);
//int c = z(wrapper);
//std::cin.ignore();
return 0;
}
错误信息如下:
error C2228: left of '.GetInt' must have class/struct/union
error C2672: 'std::mem_fn': no matching overloaded function found
error C3536: 'y': cannot be used before it is initialized
error C2064: term does not evaluate to a function taking 1 arguments
问题:
是否有可能进行这些绑定?为此我需要 std::bind
吗?
根据规范,std::mem_fn()
将成员函数指针作为参数,即
auto y = std::mem_fn(&Int::GetInt);
auto b = y(wrapper.test);
据我所知,std::mem_fn()
或多或少已经过时,因为 lambda expressions。例如
auto y = [](IntWrapper const&wrapper) { return wrapper.test.GetInt(); };
auto b = y(wrapper); // note: no need to get hold of member 'test'
你的语法错误,&IntWrapper::test
是指向成员的指针。
我没见过&IntWrapper::test.GetInt
,我什至不知道它是如何被解析的
也许这就是你想要的
auto y = std::mem_fn(&decltype(IntWrapper::test)::GetInt);
auto b = y(wrapper.test);
无论如何,std::mem_fn
的 return 类型是未指定的,我看不出为什么要使用它而不是某些 lambda 函数:
auto x = [&wrapper]{ return wrapper.GetWrappedInt(); };
int a = x();
或者:
auto x = [](const IntWrapper& wrapper){ return wrapper.GetWrappedInt(); };
int a = x(wrapper);
我什至认为这些更好,因为编译器可以有更好的优化机会。
我正在修补 std::mem_fn
,但无法将其绑定到结构成员的 data/functions(更深一层)。
我希望代码能比我描述的更好地说明问题,因为我不熟悉术语。
#include <functional>
struct Int
{
Int(int _x = 0) : x(_x) {}
int GetInt() const { return x; }
int x;
};
struct IntWrapper
{
IntWrapper(int _x = 0) : test(_x) {}
int GetWrappedInt() const { return test.GetInt(); }
Int test;
};
int main()
{
IntWrapper wrapper{ 123 };
auto x = std::mem_fn(&IntWrapper::GetWrappedInt);
//auto y = std::mem_fn(&IntWrapper::test.GetInt); // ERROR
//auto z = std::mem_fn(&IntWrapper::test.x); // ERROR
int a = x(wrapper);
//int b = y(wrapper);
//int c = z(wrapper);
//std::cin.ignore();
return 0;
}
错误信息如下:
error C2228: left of '.GetInt' must have class/struct/union
error C2672: 'std::mem_fn': no matching overloaded function found
error C3536: 'y': cannot be used before it is initialized
error C2064: term does not evaluate to a function taking 1 arguments
问题:
是否有可能进行这些绑定?为此我需要 std::bind
吗?
根据规范,std::mem_fn()
将成员函数指针作为参数,即
auto y = std::mem_fn(&Int::GetInt);
auto b = y(wrapper.test);
据我所知,std::mem_fn()
或多或少已经过时,因为 lambda expressions。例如
auto y = [](IntWrapper const&wrapper) { return wrapper.test.GetInt(); };
auto b = y(wrapper); // note: no need to get hold of member 'test'
你的语法错误,&IntWrapper::test
是指向成员的指针。
我没见过&IntWrapper::test.GetInt
,我什至不知道它是如何被解析的
也许这就是你想要的
auto y = std::mem_fn(&decltype(IntWrapper::test)::GetInt);
auto b = y(wrapper.test);
无论如何,std::mem_fn
的 return 类型是未指定的,我看不出为什么要使用它而不是某些 lambda 函数:
auto x = [&wrapper]{ return wrapper.GetWrappedInt(); };
int a = x();
或者:
auto x = [](const IntWrapper& wrapper){ return wrapper.GetWrappedInt(); };
int a = x(wrapper);
我什至认为这些更好,因为编译器可以有更好的优化机会。