473,804 Members | 3,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function pointer for 'operator>' ?

Hi guys,

I have a class defined as follows:

template <class T>
class Foo {
Foo(bool (*greater_than) (T, T));
...
};
i.e. it is a templatized class, which is given a certain binary
predicate as a comparison function.

The only problem is that I'm not sure how to construct a Foo object
using operator>.

e.g.

Foo<unsigned> foo(&(operator> ));

doesn't work as desired.

Can someone please tell me to the proper syntax?

I'm probably only going to use numerical types for T, and pass in
either operator< or operator>.

Thanks,

Joseph

Jul 29 '05 #1
5 6957

Okay, I futzed around a little bit, and now have the following:

template <class T>
class Foo {
Foo(bool (*greater_than) (const T&, const T&));
};
main() {
Foo<unsigned> foo(&greater<un signed>::operat or());
}
However, when I tried compiling it, I get the following errors:
tst.C:14: no matching function for call to `Foo<unsigned int>::Foo(bool
(std::greater<u nsigned int>::*)(const unsigned int&, const unsigned
int&)
const)'
tst.C:9: candidates are: Foo<unsigned int>::Foo(const Foo<unsigned
int>&)
tst.C:10: Foo<T>::Foo(boo l (*)(const T&, const T&))
[with T = unsigned int]

Any suggestions about how to pass the greater than function pointer
correctly?

Joseph

Jul 29 '05 #2
Joseph Turian wrote:
Any suggestions about how to pass the greater than function pointer
correctly?


There is no greater than function for predefined types. Overloaded operators
are implemented as functions, but pedefined are not.

You can use the standard comparaison functor templates (less and family), or
write something equivalent.

--
Salu2
Jul 29 '05 #3
Joseph Turian wrote:
Okay, I futzed around a little bit, and now have the following:

template <class T>
class Foo {
Foo(bool (*greater_than) (const T&, const T&));
};
main() {
Foo<unsigned> foo(&greater<un signed>::operat or());
}
Please avoid typing your examples directly into the message. Use
copy-and-paste instead.
However, when I tried compiling it, I get the following errors:
tst.C:14: no matching function for call to `Foo<unsigned int>::Foo(bool
(std::greater<u nsigned int>::*)(const unsigned int&, const unsigned
int&)
const)'
tst.C:9: candidates are: Foo<unsigned int>::Foo(const Foo<unsigned
int>&)
tst.C:10: Foo<T>::Foo(boo l (*)(const T&, const T&))
[with T = unsigned int]

Any suggestions about how to pass the greater than function pointer
correctly?


operator() in 'greater' is a non-static member, so when you take its
address, it has the type 'pointer-to-member', not 'pointer-to-function'
(see the FAQ about the difference). You might want to give your 'Foo'
template another template argument:

#include <functional>

template<class T, class G>
class Foo {
G g;
public:
Foo();
};

int main() {
Foo<unsigned, std::greater<un signed> > foo;
}

And there will be no need to pass the address of the function, just use
the 'g' object as if it were a function.

V
Jul 29 '05 #4

Victor Bazarov wrote:
Please avoid typing your examples directly into the message. Use
copy-and-paste instead.
Okay. Why, if I may ask?
operator() in 'greater' is a non-static member, so when you take its
address, it has the type 'pointer-to-member', not 'pointer-to-function'
(see the FAQ about the difference).
Got it.
For anyone else following the thread:
http://www.parashift.com/c++-faq-lit....html#faq-33.1
template<class T, class G>
class Foo {
G g;
public:
Foo();
}; And there will be no need to pass the address of the function, just use
the 'g' object as if it were a function.


Cool.

Just curious, I don't really need to initialize 'g', do I?
i.e. is there any reason to prefer:
template<class T, class G> Foo<T,G>::Foo() : g(G()) {}
compared to:
template<class T, class G> Foo<T,G>::Foo() {}
?

Thanks for your help!

Joseph

Aug 2 '05 #5
Joseph Turian wrote:
Victor Bazarov wrote:
Please avoid typing your examples directly into the message. Use
copy-and-paste instead.


Okay. Why, if I may ask?


Impossible to distinguish between the actual errors in your source
from the ones in your typing.
operator() in 'greater' is a non-static member, so when you take its
address, it has the type 'pointer-to-member', not
'pointer-to-function' (see the FAQ about the difference).


Got it.
For anyone else following the thread:
http://www.parashift.com/c++-faq-lit....html#faq-33.1
template<class T, class G>
class Foo {
G g;
public:
Foo();
};

And there will be no need to pass the address of the function, just
use the 'g' object as if it were a function.


Cool.

Just curious, I don't really need to initialize 'g', do I?
i.e. is there any reason to prefer:
template<class T, class G> Foo<T,G>::Foo() : g(G()) {}
compared to:
template<class T, class G> Foo<T,G>::Foo() {}
?


No reason. There is no reason to provide an empty constructor either.
The compiler can do it for you.

V
Aug 2 '05 #6

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

Similar topics

3
4765
by: glen stark | last post by:
Hi all. I'm working with an array of member function pointers (they are all get function of the class Bead). The typedef is: typedef _real (Bead::*_beadGfp)(void); I have a class System which contains both an array of function pointers: std::vector<_beadGfp> msmtFPs;
5
2124
by: Roger Leigh | last post by:
I've written a simple container template class to contain a single value. This emits a signal when the value is changed (it's used as a notifier of changes), and listeners can connect to its changed signal. i.e. field<int> i(2); i = 4; // field<int>::m_value = 4; changed signal is emitted. Currently, the contained value may be accessed via get_value() and set_value() methods, and for ease of use, operator= and some type conversions...
3
2055
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look fine? // --------- foo.cpp --------- #include <iostream> using namespace std;
6
1235
by: Michael Hopkins | last post by:
Hi all I have a class that contains as one of it's members a type that is internally constructed like this: std::vector< std::valarray< float > > foo Think of it as a matrix. I cannot construct foo when the class itself is constructed because I don't know how big it needs to be until I have loaded that information from a file.
19
2515
by: Rajesh S R | last post by:
Consider the following code: int main() { struct name { long a; int b; long c; }s={3,4,5},*p; p=&s;
14
2817
by: Jess | last post by:
Hi, I read about operator overloading and have a question regarding "operator->()". If I have two classes like this: struct A{ void f(); }; struct B{
6
2941
by: edd | last post by:
Hello all, Is there a way to determine whether a particular type supports the -> operator at compile time? I'm trying to write a template function (or a series of overloads) that will yield the raw pointer at "the end of the arrow". For types that support the operator, I have a reasonable solution, but I'd like to have a null pointer returned if the operator isn't supported by a particular object.
9
2927
by: Nico | last post by:
I want to create a const_iterator that gives me access to dynamically generated objects. For example see the class Polygon that contains a list of points stored as following doubles in a vector (x1, y1, x2, y2, ...). The iterator should return only pairs of doubles to avoid possible confusion of the user of the class Polygon. (The storage format is needed for conversion to other libraries.) The code for Polygon below compiles well, but...
6
1936
by: puzzlecracker | last post by:
Say we have this structure: Struct Foo{ .... friend ostream& operator << (ostream& s, Foo & m); ..... }; friend ostream& operator << (ostream& s, Foo & m){
0
9704
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
9571
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10561
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
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6845
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5505
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
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
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
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.