Someone on stack overflow wrote an interesting way to capture a lambda or functor into your own class. I was trying to simplify it, and I think I got close but was having some trouble. Their example was:
// OT => Object Type
// RT => Return Type
// A ... => Arguments
template<typename OT, typename RT, typename ... A>
struct lambda_expression {
OT _object;
RT(OT::*_function)(A...)const; // A pointer to a member function,
// specifically the operator()
lambda_expression(const OT & object) // Constructor
: _object(object),
_function(&decltype(_object)::operator()) {} // Assigning the function pointer
RT operator() (A ... args) const {
return (_object.*_function)(args...);
}
};
Basically this allows you to go:
int captureMe = 2;
auto lambda = [=](int a, int b) { return a + b + captureMe;};
lambda_expression<decltype(lambda), int, int, int>(lambda);
I was trying to simplify this, and thought that the pointer contained in the lambda_expression class wouldn't be needed, because you can call the function object itself, instead of calling the pointer to the operator(). So I tried this:
template <typename OT, typename ... Args> // No Return type specified
struct lambdaContainer
{
lambdaContainer(OT funcObj) : funcObj(funcObj){ }
OT funcObj; // No pointer, just the function object.
auto operator()(Args... args)
{
return funcObj(args...); // Call the function object directly
}
};
Then something like:
int captureMe = 2;
auto lambda = [=](int a, int b) { return a + b + captureMe; };
lambdaContainer<decltype(lambda), int, int> lam(lambda);
auto i = lam(1, 1);
// i = 4;
Where I wrote the line:
auto operator()(Args... args)
{
return funcObj(args...);
}
Apparently:
decltype(auto) operator()(Args... args) //works in C++14 apparently.
But I tried without the auto keyword and I failed miserably in doing this, I want to understand how the Args... works. I tried:
decltype(funObj(Args...) operator()(Args... args) // this failed
decltype(OT(Args...) operator() (Args... args) // this failed
auto operator() (Args... args) -> decltype(funcObj(Args...)) // this failed
auto operator() (Args... args) -> decltype(OT(Args...)) // this failed
How can I expand the Args parameter so the template can deduce the return type? Is this only possible with auto?