By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,486 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

Function pointer for 'operator>' ?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

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<unsigned>::operator());
}
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<unsigned 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(bool (*)(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

P: n/a
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

P: n/a
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<unsigned>::operator());
}
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<unsigned 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(bool (*)(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<unsigned> > 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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.