473,507 Members | 6,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class template parameter that is a function

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
Jul 23 '05 #1
6 9241
* 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;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Brian Ross wrote:
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;
}

Jul 23 '05 #3
Brian Ross wrote:
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
Jul 23 '05 #4
Rolf Magnus wrote:
[...]
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
Jul 23 '05 #5
Brian Ross wrote:
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...on_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?

--
Matthias Kaeppler
Jul 23 '05 #6
Victor Bazarov wrote:
int main()
{
CObjectT(Func1) Test1;


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);

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3904
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
13
2797
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
7
2115
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
2
1657
by: Bjoern Knafla | last post by:
Hi! I am trying to specialize a member function of a template class (see code below). The moment the header with the "template class definition" and "template class member function...
9
8232
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
6
1809
by: PengYu.UT | last post by:
Hi, I run into error with the following program. Would you please help me? Best wishes, Peng struct tag1{}; struct tag2{};
17
2196
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
3
3557
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
6
391
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.