Prev: Is this correct C++?
Next: Simplester MetaLoop
From: tohava on 29 Oct 2009 05:17 On Oct 29, 1:13 am, PGK <graham.k...(a)gmail.com> wrote: > My question is, can I reconfigure this code so that I don't have to > explicitly specify the types of the two function pointers? For > example, something "like": Loop2<2>::foo(f2,f1); Just add this: template<int N, typename BASE_FUNC, typename INDUCTIVE_FUNC> void Repeat(BASE_FUNC bFunc, INDUCTIVE_FUNC iFunc) { Loop2<BASE_FUNC, INDUCTIVE_FUNC, N>::foo(bFunc, iFunc); } Which can be called like this: Repeat<2>(f2, f1); -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Paul Bibbings on 29 Oct 2009 15:27
PGK wrote: > I've written a simple template metaprogramming loop (below) using a > struct template which requires a loop index, and two function > signatures; one for the base (zero) case, and one for the inductive > (ith) case. As you can see below, I start things off with a call like > this: > > Loop2<double (*)(double), void (*)(int), 2 >::foo(f2,f1); > > and the output is: > > Hello from f1(2). > Hello from f1(1). > Hello from f2(0.123). > > My question is, can I reconfigure this code so that I don't have to > explicitly specify the types of the two function pointers? For > example, something "like": Loop2<2>::foo(f2,f1); You could provide a simple helper function that re-orders the template parameters and uses template argument deduction to deduce the function pointers for you. As, for example: // ... template<int i, typename BaseFunc, typename InductiveFunc> void loop2_foo(BaseFunc bFunc, InductiveFunc iFunc) { Loop2<BaseFunc, InductiveFunc, i>::foo(bFunc, iFunc); } int main() { loop2_foo<2>(f2, f1); // ... } Regards Paul Bibbings -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |