Problem
Have a template that generates a wrapper for an arbitrary member function that allows to invoke that function on an object, e.g.:
Given bool std::string::empty()
, the wrapper to be generated would be bool wrapper(const std::string& str) { return str.empty(); }
.
The wrapper function shall not be a lambda (so that it can be passed as function pointer).
My approach (for non-const member functions)
/* Wrapper (for non-const member functions) */
template<typename T, typename R, typename... ARGS>
struct staticWrapperHelper
{
using fType = R (T::*)(ARGS...);
template<fType FUNCTION>
static R wrapper(T& object, ARGS... args) { return object.*FUNCTION(args...); }
};
template<typename T, typename R, typename... ARGS>
auto staticWrapper(R (T::*memberFunction)(ARGS...))
{
//auto function = [memberFunction] (T& object, ARGS... args) -> R { return (object.*memberFunction)(args...); };
using Helper = staticWrapperHelper<T, R, ARGS...>;
R (*wrapper)(T&, ARGS...) = &Helper::template wrapper<R (T::*)(ARGS...)>;
return wrapper;
}
/* Example */
class A
{
public:
void member() {}
};
int main()
{
A a;
auto f = staticWrapper<A>(&A::member);
f(a);
return 0;
}
Issue with my approach
It does not compile. It seems like the returned wrapper is still a template: http://coliru.stacked-crooked.com/a/d4c0dd9ab631aa1f
g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out main.cpp: In instantiation of 'auto staticWrapper(R (T::*)(ARGS ...)) [with T = A; R = void; ARGS = {}]': main.cpp:32:41: required from here main.cpp:17:9: error: no matches converting function 'wrapper' to type 'void (*)(class A&)' 17 | R (*wrapper)(T&, ARGS...) = &Helper::template wrapper; | ^~~~~~~ main.cpp:8:14: note: candidate is: 'template static R staticWrapperHelper::wrapper(T&, ARGS ...) [with R (T::* FUNCTION)(ARGS ...) = FUNCTION; T = A; R = void; ARGS = {}]' 8 | static R wrapper(T& object, ARGS... args) { return object.*FUNCTION(args...); } | ^~~~~~~
Any ideas?