Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Feedback on this template function

Reply
Thread Tools

Feedback on this template function

 
 
JohanS
Guest
Posts: n/a
 
      08-25-2004
I have been trying to create this function for some time now. Reading
about pointers to member functions until my head hurts. Now i did it
like you see below and i am now wondering. What am i missing here?
What am i not thinking about? Cause this way was to easy. Will i get
in trouble in the future doing it this way? Feedback appreciated! Is
there something that can be done alot better (I'm not looking for a
totally different way of doing it)?


#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))

namespace misc {
template <typename Iterator, typename Function>
int count(Iterator start, Iterator end, Function fun, int value)
{
int temp = 0;

while(start != end)
{
if(CALL_MEMBER_FN(*start, fun) () == value)
++temp;

++start;
}

return temp;
}}

// In main -> works.. wohoo
cout << misc::count(vl.begin(), vl.end(),
&SomeClass::SomeMemberFunction, 3);
 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      08-25-2004
"JohanS" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I have been trying to create this function for some time now. Reading
> about pointers to member functions until my head hurts. Now i did it
> like you see below and i am now wondering. What am i missing here?
> What am i not thinking about? Cause this way was to easy. Will i get
> in trouble in the future doing it this way? Feedback appreciated! Is
> there something that can be done alot better (I'm not looking for a
> totally different way of doing it)?
>
>
> #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
>
> namespace misc {
> template <typename Iterator, typename Function>
> int count(Iterator start, Iterator end, Function fun, int value)
> {
> int temp = 0;
>
> while(start != end)
> {
> if(CALL_MEMBER_FN(*start, fun) () == value)
> ++temp;
>
> ++start;
> }
>
> return temp;
> }}
>
> // In main -> works.. wohoo
> cout << misc::count(vl.begin(), vl.end(),
> &SomeClass::SomeMemberFunction, 3);


The macro is unnecessary, and macros are generally frowned upon because they
can cause headaches, so consider removing it. Also, you could probably do
the same thing using std::count_if (<algorithm>) and some of the stuff
provided in <functional> or boost's library, but I wouldn't call such an
approach "better" because it can be confusing. I suppose that you could
also make the type of the last parameter a template argument (e.g. "T value"
instead of "int value"), but it's not necessary if you're only dealing with
ints. Is there anything that you do not like about the code?

--
David Hilsee


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Feedback from feedback on MCP questions Matt Adamson Microsoft Certification 0 04-27-2009 11:13 AM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Template function as argument to another template function Jim West C++ 3 10-07-2004 07:21 PM
function non-template versus fully specialized function template Robert Allan Schwartz C++ 1 08-09-2004 03:41 PM



Advertisments