For a given struct:
struct foo
{
void fooFunc(){}
int fooVar = 0;
};
I can create a call wapper to the function: std::mem_fn( &foo::fooFunc )
such that I can pass it into another method and call it on an object.
I want to know if there is a similar call wrapper but for member variables.
For example I'm using a pointer to a member variable here and but I'd like to use an call wrapper:
void bar( std::function< void( foo ) > funcPtr, int foo::* varPtr )
{
foo myFoo;
funcPtr( myFoo );
foo.*varPtr = 13;
}
N.B. Nothing in your question is from the STL (which is a library from the 1990s), std::function
and std::mem_fn
are part of the C++ Standard Library, which is not the same thing.
std::mem_fn
supports member functions and member variables, so you can just do this to access the member variable and set it:
foo f;
std::function<int&(foo&)> var( std::mem_fn( &foo::fooVar ) );
var(f) = 1;