Feedback on this template function

Discussion in 'C++' started by JohanS, Aug 25, 2004.

  1. JohanS

    JohanS Guest

    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);
    JohanS, Aug 25, 2004
    #1
    1. Advertising

  2. JohanS

    David Hilsee Guest

    "JohanS" <> wrote in message
    news:...
    > 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
    David Hilsee, Aug 25, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brent
    Replies:
    2
    Views:
    820
    Brent
    Oct 24, 2005
  2. Robert Allan Schwartz
    Replies:
    1
    Views:
    394
    Victor Bazarov
    Aug 9, 2004
  3. Jim West
    Replies:
    3
    Views:
    756
    Jim West
    Oct 7, 2004
  4. Vijai Kalyan
    Replies:
    4
    Views:
    701
    Vijai Kalyan
    Nov 8, 2005
  5. Peng Yu
    Replies:
    3
    Views:
    767
    Thomas J. Gritzan
    Oct 26, 2008
Loading...

Share This Page