473,782 Members | 2,494 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 9269
* 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()(doub le 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.Functiona l,
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
3917
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
2828
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 { }; template <typename Base> class Tpl : protected Base {
7
2132
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
1669
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 specialization" is included in two different translation units (cpp-files). Problem:
9
8256
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 solution? Declaring the function extern "C" fails, because linkage declarations must be made at file scope. #include <stdlib.h> //for qsort template <class T> class Sorter
6
1836
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
2237
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 an algorithm that modifies this datastructure. The basic outline of the algorithm is independent of the type of property. So I implemented a generic version of the algorithm and a function object for
3
3584
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 be instantiated. The user does not have
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
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10311
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5378
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.