C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Function pointer for 'operator>' ?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Joseph Turian
Guest





PostPosted: Fri Jul 29, 2005 9:54 pm    Post subject: Function pointer for 'operator>' ? Reply with quote



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





PostPosted: Fri Jul 29, 2005 10:07 pm    Post subject: Re: Function pointer for 'operator>' ? Reply with 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<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





PostPosted: Fri Jul 29, 2005 10:16 pm    Post subject: Re: Function pointer for 'operator>' ? Reply with quote



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





PostPosted: Fri Jul 29, 2005 10:21 pm    Post subject: Re: Function pointer for 'operator>' ? Reply with quote

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





PostPosted: Tue Aug 02, 2005 4:38 am    Post subject: Re: Function pointer for 'operator>' ? Reply with quote


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





PostPosted: Tue Aug 02, 2005 5:02 am    Post subject: Re: Function pointer for 'operator>' ? Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.