stl for_each makes two additional copies of functor

Discussion in 'C++' started by Sean, Jun 30, 2003.

  1. Sean

    Sean Guest

    #include <iostream>
    #include <algorithm>
    struct functor {
    ~functor() {std::cout << 'D';}
    void operator()(int a) {std::cout << a;}
    };
    int main()
    {
    int a[] = {1,2,3,4,5};
    functor f;
    std::for_each(a,a+5,f);
    }
    // produces : 12345DDD

    notice the three D's in the output.

    The functor is copied once as pass-by-value to for_each()
    and once again as the return value from for_each().

    Is there a way to use for_each without introducing these
    extra copies of the functor? Perhaps this is just a
    g++ implementation issue?

    Regards,
    Sean
     
    Sean, Jun 30, 2003
    #1
    1. Advertising

  2. "Sean" <> wrote...
    > #include <iostream>
    > #include <algorithm>
    > struct functor {
    > ~functor() {std::cout << 'D';}
    > void operator()(int a) {std::cout << a;}
    > };
    > int main()
    > {
    > int a[] = {1,2,3,4,5};
    > functor f;
    > std::for_each(a,a+5,f);
    > }
    > // produces : 12345DDD
    >
    > notice the three D's in the output.
    >
    > The functor is copied once as pass-by-value to for_each()
    > and once again as the return value from for_each().
    >
    > Is there a way to use for_each without introducing these
    > extra copies of the functor? Perhaps this is just a
    > g++ implementation issue?



    Try

    int a[] = {1,2,3,4,5};
    const functor &f = functor();
    std::for_each(a,a+5,f);

    Victor
     
    Victor Bazarov, Jun 30, 2003
    #2
    1. Advertising

  3. Sean

    Sean Guest

    "Howard Hinnant" <> wrote in message
    news:300620031912177840%...
    <snip>
    >
    > You could use explicit template arguments to specify pass-by-reference
    > for for_each:
    >
    > #include <iostream>
    > #include <algorithm>
    >
    > struct functor
    > {
    > ~functor() {std::cout << 'D';}
    > void operator()(int a) const {std::cout << a;}
    > };
    >
    > int main()
    > {
    > int a[] = {1,2,3,4,5};
    > functor f;
    > std::for_each<int*, functor&>(a,a+5,f);
    > }
    >
    > 12345D
    >
    > Not all algorithms may behave as you want, even with this extra effort.
    > For example see:
    >
    > http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-active.html#92
    >
    > However, if you happen to be a Metrowerks customer, and you're getting
    > unwanted functor copying even when passing by reference via explicit
    > template arguments, feel free to report it as a bug to me. We endeavor
    > to make this idiom work.
    >
    > --
    > Howard Hinnant
    > Metrowerks


    Thanks for this helpful information Howard. It seems to me that
    pass-by-reference should be the default behavior for for_each. But after
    considering the information at the link you provided i can understand
    why it is not.

    Cheers,
    Sean
     
    Sean, Jul 1, 2003
    #3
    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. Alex Vinokur
    Replies:
    5
    Views:
    614
    Alex Vinokur
    Nov 4, 2003
  2. Michael

    STL for_each

    Michael, Feb 8, 2005, in forum: C++
    Replies:
    4
    Views:
    5,033
    Michael
    Feb 8, 2005
  3. Eric Lilja
    Replies:
    15
    Views:
    544
    Eric Lilja
    May 28, 2007
  4. nguillot
    Replies:
    14
    Views:
    2,355
    nguillot
    Jan 29, 2008
  5. mthread
    Replies:
    4
    Views:
    463
    James Kanze
    Jun 1, 2009
Loading...

Share This Page