Function pointer for 'operator>' ?

J

Joseph Turian

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
 
J

Joseph Turian

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>::eek:perator());
}


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
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Joseph said:
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.
 
V

Victor Bazarov

Joseph said:
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>::eek:perator());
}

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
 
J

Joseph Turian

Victor said:
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-lite/pointers-to-members.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
 
V

Victor Bazarov

Joseph said:
Okay. Why, if I may ask?

Impossible to distinguish between the actual errors in your source
from the ones in your typing.
Got it.
For anyone else following the thread:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.1



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
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top