Class template parameter that is a function

B

Brian Ross

Hi,

I am trying to do something similar to the following:

int Func1(int x, int y);
double Func2(double x, double y);

template <typename FuncT> // <- What would go here
class CObjectT
{
int f()
{ return FuncT(1, 2); }
};

int main()
{
CObjectT<Func1> Test1;
Test1.f();

CObjectT<Func2> Test2;
Test2.f();

return 0;
}

I am pretty sure that it isn't working because the template parameter needs
to be a type and I am essentially passing a value (of a function). I am also
pretty sure that I can have a function pointer as a parameter but this will
only work if all the functions that will be used as a template parameter
have the same signature - which they wont. I want to be able to have any
function as long as it takes two parameters and returns an 'int'.

Is there any way to do something like the above where I only need to specify
the function as a template parameter and it will compile as long as the code
uses the function correctly?

Thanks
Brian
 
A

Alf P. Steinbach

* Brian Ross:
I am trying to do something similar to the following:

int Func1(int x, int y);
double Func2(double x, double y);

template <typename FuncT> // <- What would go here
class CObjectT
{
int f()
{ return FuncT(1, 2); }
};

int main()
{
CObjectT<Func1> Test1;
Test1.f();

CObjectT<Func2> Test2;
Test2.f();

return 0;
}

[...] I want to be able to have any
function as long as it takes two parameters and returns an 'int'.

That's different from your declared functions, but OK:

int Func1(int x, int y){ return 0; }
int Func2(double x, double y){ return 0; }

template< typename X, typename Y, int Func( X, Y ) >
class CObjectT
{
public:
int f() const { return Func(1, 2); }
};

int main()
{
CObjectT<int, int, Func1> Test1;
Test1.f();

CObjectT<double, double, Func2> Test2;
Test2.f();

return 0;
}
 
R

Rolf Magnus

Brian said:
Hi,

I am trying to do something similar to the following:

int Func1(int x, int y);
double Func2(double x, double y);

template <typename FuncT> // <- What would go here
class CObjectT
{
int f()
{ return FuncT(1, 2); }
};

int main()
{
CObjectT<Func1> Test1;
Test1.f();

CObjectT<Func2> Test2;
Test2.f();

return 0;
}

I am pretty sure that it isn't working because the template parameter
needs to be a type and I am essentially passing a value (of a function).

You are passing a pointer to function. There is no such thing as a "value of
a function".
I am also pretty sure that I can have a function pointer as a parameter
but this will only work if all the functions that will be used as a
template parameter have the same signature - which they wont.

Then combine it. Pass a function pointer and make its type a template
parameter.

template <typename FuncT> // <- What would go here
class CObjectT
{
public:
CObjectT(FuncT func)
: func_(func)
{}

int f()
{ return func_(1, 2); }
private:
FuncT func_;
};

int main()
{
CObjectT(Func1) Test1;
Test1.f();

CObjectT(Func2) Test2;
Test2.f();

return 0;
}

This also has the advantage that it works with function objects, too.
I want to be able to have any function as long as it takes two parameters
and returns an 'int'.

Your second example function returns a double, not an int.
Is there any way to do something like the above where I only need to
specify the function as a template parameter and it will compile as long
as the code uses the function correctly?

I don't think so. You could do it with function objects, instantiating the
function object within f(). Something like:

struct Func1
{
int operator()(int x, int y);
};

struct Func2
{
double operator()(double x, double y);
};

template <typename FuncT> // <- What would go here
class CObjectT
{
int f()
{ return FuncT()(1, 2); }
};

int main()
{
CObjectT<Func1> Test1;
Test1.f();

CObjectT<Func2> Test2;
Test2.f();

return 0;
}
 
V

Victor Bazarov

Brian said:
I am trying to do something similar to the following:

int Func1(int x, int y);
double Func2(double x, double y);

template <typename FuncT> // <- What would go here
class CObjectT
{
int f()

BTW, your 'f' is private here...
{ return FuncT(1, 2); }
};

int main()
{
CObjectT<Func1> Test1;

You're passing the address of a function here, while it expects a type.
Test1.f();

CObjectT<Func2> Test2;
Test2.f();

return 0;
}

I am pretty sure that it isn't working because the template parameter
needs to be a type and I am essentially passing a value (of a
function).

You're passing a function which decays to a pointer to a function, I
believe.
I am also pretty sure that I can have a function pointer
as a parameter but this will only work if all the functions that will
be used as a template parameter have the same signature - which they
wont. I want to be able to have any function as long as it takes two
parameters and returns an 'int'.

But your 'Func2' returns a 'double'. It won't fit the requirements.
Is there any way to do something like the above where I only need to
specify the function as a template parameter and it will compile as
long as the code uses the function correctly?

Are you actually sure you want to pass the function as the template
argument and not, say, as the argument to the constructor or the 'f'
member?

V
 
V

Victor Bazarov

Rolf said:
[...]
Then combine it. Pass a function pointer and make its type a template
parameter.

template <typename FuncT> // <- What would go here
class CObjectT
{
public:
CObjectT(FuncT func)
: func_(func)
{}

int f()
{ return func_(1, 2); }
private:
FuncT func_;
};

int main()
{
CObjectT(Func1) Test1;

I've never seen syntax like that. Can you enlighten as to what it's
supposed to do?
Test1.f();

CObjectT(Func2) Test2;
Test2.f();

return 0;
}

V
 
M

Matthias Kaeppler

Brian said:
Hi,

I am trying to do something similar to the following:

int Func1(int x, int y);
double Func2(double x, double y);

template <typename FuncT> // <- What would go here
class CObjectT
{
int f()
{ return FuncT(1, 2); }
};

int main()
{
CObjectT<Func1> Test1;
Test1.f();

CObjectT<Func2> Test2;
Test2.f();

return 0;
}

I am pretty sure that it isn't working because the template parameter needs
to be a type and I am essentially passing a value (of a function). I am also
pretty sure that I can have a function pointer as a parameter but this will
only work if all the functions that will be used as a template parameter
have the same signature - which they wont. I want to be able to have any
function as long as it takes two parameters and returns an 'int'.

Is there any way to do something like the above where I only need to specify
the function as a template parameter and it will compile as long as the code
uses the function correctly?

Thanks
Brian

Yes, you can indeed pass a function pointer as a template argument:

template< typename RetT, typename ArgT, RetT f(ArgT) >
class A
{
// ...
};

int foo(int a)
{
// ...
}

int main()
{
A<int,int,foo> a;
}


But there is a more elegant solution, which also allows you to pass a
function pointer OR a functor to your class. Look up Boost.Functional,
especially http://www.boost.org/libs/functional/function_traits.html

Hope that helps.


PS: I noticed that in the code above, 'void' may not be passed as a
template argument. Does anyone know why?
 
R

Rolf Magnus

Victor said:
I've never seen syntax like that. Can you enlighten as to what it's
supposed to do?

It's supposed to be a typo. ;-)
It should have been:

CObjectT Test1(Func1);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,664
Latest member
RoseannBow

Latest Threads

Top