How to discover argument and result types from lambda for
constructing std::function?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi!
I have a class template that contains a boost::function member
variable (shall be std::function in future, but gcc4.6 does not have
it.) The function depends on the template arguments of the class
template. For constructing an instance of the class template I added
a free function template that shall be able to discover the template
arguments---just like std::make_pair does.
The problem lies in an easy definition of a function template that
will accept a lambda type, deduce the argument and result types, and
construct the correct instantiation of the class template. I have
played around with decltype, but I'm not quite happy with my solution.
Is there any easier solution? Are there any predefined type traits or
something to make this shorter? Is relying on &F::operator() fine for
lambda types F?
//code works on gcc 4.6:
#include <boost/function.hpp>
#include <utility>
using boost::function;
/* class to hold some function */
template<typename Arg, typename Result>
struct Holder {
typedef function<Result(Arg)> Func;
Func f;
Holder(Func && newF) : f(std::move(newF)) {}
//...
};
/* free factory method to deduce template arguments */
template<typename Arg, typename Result>
Holder<Arg, Result> mkHolder(function<Result(Arg)> && f)
{
return Holder<Arg, Result>(std::move(f));
}
template<typename F>
struct discover_f_type;
template<typename T, typename Arg, typename Result>
struct discover_f_type<Result (T::*)(Arg) const>
{
typedef boost::function<Result(Arg)> function_type;
};
/* how to deduce template arguments from lambda? */
template<typename F>
auto mkHolder(F && f) -> decltype(mkHolder(
typename discover_f_type<decltype(&F::operator())>
::function_type(f)
))
{
typedef discover_f_type<decltype(&F::operator())> D;
return mkHolder(typename D::function_type(f));
}
int main()
{
// works:
mkHolder([](int const i) { return i+5; });
}
Frank
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: keyserver x-hkp://pool.sks-keyservers.net
iEYEARECAAYFAk8pwU4ACgkQhAOUmAZhnmqJBQCfQB4qSjtCKEwS6hT8QGRqPJjU
lGQAoJIUI8qaFHHD48CEfB+HrLmCPteL
=drM6
-----END PGP SIGNATURE-----
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]