Thread: for_each function and operator()

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    for_each function and operator()

    Hello everyone,


    I can not find related information in MSDN, so I want to confirm here, that,

    the implementation of for_each is something like,

    Code:
    template <class A, class B> B for_each (A begin, A end, B Func)
    {
        while (begin != end) Func(*begin++);
        return Func;
    }
    So, operator(*A) of class Func will be invoked? Right?


    thanks in advance,
    George
    Last edited by George2; 12-03-2007 at 07:26 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    the implementation of for_each is something like
    The C++ Standard declares std::for_each() as:
    Code:
    template<class InputIterator, class Function>
    Function for_each(InputIterator first, InputIterator last, Function f);
    The MinGW port of g++ 3.4.5 has an implementation that amounts to:
    Code:
    template<class InputIterator, class Function>
    Function for_each(InputIterator first, InputIterator last, Function f)
    {
        for (; first != last; ++first)
        {
            f(*first);
        }
        return f;
    }
    So, operator(*A) of class Func will be invoked? Right?
    Function::operator(InputIterator::value_type) is the overloaded operator that will be invoked.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    So, operator(*A) of class Func will be invoked? Right?
    Actually, operator()(typename std::iterator_traits<A>::value_type) will be invoked.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I seems to me that Func doesn't necessarily have to be a function object but might be a simple function with a suitable prototype.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I seems to me that Func doesn't necessarily have to be a function object but might be a simple function with a suitable prototype.
    Yes, that is true.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM