| re: Passing pointer to template function as argument to pointer to template function
"Vijai Kalyan" <vijai.kalyan@gmail.com> wrote in message
news:1131461765.070607.327110@o13g2000cwo.googlegr oups.com[color=blue]
> I was decomposing a task into different policies. Essentially, there
> is a general option obtained from a server and user options obtained
> from configuration variables. The two options are complementary to one
> another.
>
> So I proceeded to decompose the tasks that deal with user options into
> two functions. Each of the functions do something and towards the end
> they do supplementary tasks that depend on the server option.
>
> The whole things fits in a framework which notifies listeners before
> and after the main tasks and so the notification scheme for the
> listeners are separate policies as well.
>
> The whole thing is combinatorially complex and hence my decision to
> split things up using templates as policies. Here is where I run into
> my question: It is exemplified by the following simple code:
>
> #include <iostream>
>
> // main task function
> template<class FuncPtr>
> void Foo(FuncPtr ptr)
> {
> ptr();
> }
>
> // want to pass an instance of this to Foo
> template<class X>
> void Bar(X x)
> {
> x();
> }[/color]
In that case, Foo is defined wrongly. If you pass an instance of Bar to Foo,
then ptr will be &Bar<something> so
ptr();
translates into
Bar<something>();
which is wrong because Bar<something> has an X parameter. You must call:
ptr(x);
However, your Foo function isn't set up to be able to make sense of this x
argument. Try this
template<class BarFuncPtr, class GFunctPtr>
void Foo(BarFuncPtr bptr, GFunctPtr gptr)
{
bptr(gptr);
}
// want to pass an instance of this to Foo
template<class X>
void Bar(X x)
{
x();
}
// want to pass pointer to this to Bar --- via Foo
void G()
{
std::wcout << "From G!" << std::endl;
}
int main()
{
typedef void (*GPtr) ();
Foo(&Bar<GPtr>, &G);
return 0;
}
--
John Carson |