I would like some way to get the first parameter type of a lambda function, is this possible?
e.g.
instead of:
template<typename T>
struct base
{
virtual bool operator()(T) = 0;
}
template<typename F, typename T>
struct filter : public base<T>
{
virtual bool operator()(T) override {return /*...*/ }
};
template<typename T, typename F>
filter<T> make_filter(F func)
{
return filter<F, T>(std::move(func));
}
auto f = make_filter<int>([](int n){return n % 2 == 0;});
I would like:
template<typename F>
struct filter : public base<typename param1<F>::type>
{
bool operator()(typename param1<F>::type){return /*...*/ }
};
template<typename F>
filter<F> make_filter(F func)
{
return filter<F>(std::move(func));
}
auto f = make_filter([](int n){return n % 2 == 0;});
Based on Xeo's answer this is what I got working in VS2010:
template<typename FPtr>
struct arg1_traits_impl;
template<typename R, typename C, typename A1>
struct arg1_traits_impl<R (C::*)(A1)>{typedef A1 arg1_type;};
template<typename R, typename C, typename A1>
struct arg1_traits_impl<R (C::*)(A1) const>{typedef A1 arg1_type;};
template<typename T>
typename arg1_traits_impl<T>::arg1_type arg1_type_helper(T);
template<typename F>
struct filter : public base<typename std::decay<decltype(detail::arg1_type_helper(&F::operator()))>::type>
{
bool operator()(typename std::decay<decltype(detail::arg1_type_helper(&F::operator()))>::type){return /*...*/ }
};
template<typename T, typename F>
filter<F> make_filter(F func)
{
return filter<F>(std::move(func));
}
I've tried simplifying the the code, but any attempt seems to break it.
std::fuction::first_argument_type
, however VS2010 doesn't seem to implemenetfirst_argument_type
. – Hygrophilousfunction_traits<decltype(&F::operator())>::param1_type
or the like. VS2010 has problems with that code though, let me see if I can find my question about it... – Eleaseeleaticstd::function<F>
for that, asF
is the lambda type and not a signature likebool(int)
. – Eleaseeleaticoperator()(T)
which needs to know the argument type. – Hygrophiloustemplate<class Arg> bool operator()(Arg&& arg){ _f(std::forward<Arg>(arg)); }
where_f
is the stored function. – Eleaseeleatic