I was tinkering around with the std::mem_fn
and couldn't manage to bind it to data/functions of an member of a struct (one layer deeper).
I hope that the code shows the problem better than I can describe it, because I'm not familiar with the terminology.
#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;
}
The error message is the following:
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
Question:
Is it possible to make these binds? Do I need std::bind
for this?
y = [](const IntWrapper& w){ return w.test.x; };
– Apostil