 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Joseph Turian Guest
|
Posted: Fri Jul 29, 2005 9:54 pm Post subject: 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
|
|
| Back to top |
|
 |
Joseph Turian Guest
|
Posted: Fri Jul 29, 2005 10:07 pm Post subject: Re: Function pointer for 'operator>' ? |
|
|
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
|
|
| Back to top |
|
 |
Julián Albo Guest
|
Posted: Fri Jul 29, 2005 10:16 pm Post subject: Re: Function pointer for 'operator>' ? |
|
|
Joseph Turian wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Jul 29, 2005 10:21 pm Post subject: Re: Function pointer for 'operator>' ? |
|
|
Joseph Turian wrote:
| Quote: | 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
}
|
Please avoid typing your examples directly into the message. Use
copy-and-paste instead.
| Quote: | 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 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
|
|
| Back to top |
|
 |
Joseph Turian Guest
|
Posted: Tue Aug 02, 2005 4:38 am Post subject: Re: Function pointer for 'operator>' ? |
|
|
Victor Bazarov wrote:
| Quote: | Please avoid typing your examples directly into the message. Use
copy-and-paste instead.
|
Okay. Why, if I may ask?
| Quote: | 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
| Quote: | 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
compared to:
template<class T, class G> Foo<T,G>::Foo() {}
?
Thanks for your help!
Joseph
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Aug 02, 2005 5:02 am Post subject: Re: Function pointer for 'operator>' ? |
|
|
Joseph Turian wrote:
| Quote: | 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.
| Quote: | 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
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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|