Re: Variadic templates in C++0x
On May 14, 7:56 pm, Olivier <olivier.gr...@gmail.com> wrote:
template< typename F0, typename F1 >
class DoubleFunctionCall;
template< typename Ret0, typename ... Args0, typename Ret1,
typename ... Args1 >
class DoubleFunctionCall<Ret0 (Args0 ...), Ret1 (Args1 ...)>
{
public:
typedef Ret0 (*F0Type)( Args0 ... );
typedef Ret1 (*F1Type)( Args1 ... );
DoubleFunctionCall( F0Type f0, F1Type f1 )
: _M_f0(f0), _M_f1(f1)
{}
void operator()( Args0 && ... args0, Args1 && ... args1 )
{
(*_M_f0)(std::forward<Args0>(args0) ...);
(*_M_f1)(std::forward<Args1>(args1) ...);
}
private:
F0Type _M_f0;
F1Type _M_f1;
};
If you try and compile this code with ConceptGCC, you'll actually get
an error message saying:
error: parameter packs must be at the end of the parameter list
Something like this might work (apologies for syntax errors, mistakes
in lambda notation, etc -- I don't have a compiler for this
language!):
// Define currying combinator
template<typename F> struct Curry;
template<typename R, typename Arg0, typename ... Args> struct
Curry<R(Arg0, Args...)>
{
Curry(const std::function<R(Arg0, Args...)> &f) : f(f) {}
Curry<R(Args)> operator()(Arg0 &&first) { return Curry<R(Args)>( []
(Args &&...rest) -> { f(first, rest...) } }
private:
std::function<R(Arg0, Args...)> f;
};
template<typename R, typename Arg0> struct Curry<R<Arg0>>
{
//...
R operator()(Arg0 &&first) { return f(r); }
};
template<typename R, typename ... Args> Curry<R(Args...)> curry(const
std::function<R(Args...)> &f);
// Define uncurrying combinator
template<typename F> struct Uncurry;
// ... likewise ...
template<typename R, typename R2, typename ... Args>
std::function<R(Args...)> forceReturn(const std::function<R2(Args...)>
&f, R &&r) { return [] (Args... &&a) -> { f(a...); return r; } }
//... then ...
auto fAndG = uncurry(curry(forceReturn(f, curry(g))));
That ought to work. There's probably a simpler way, though.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]