stl for_each makes two additional copies of functor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sean

    stl for_each makes two additional copies of functor

    #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



  • Victor Bazarov

    #2
    Re: stl for_each makes two additional copies of functor

    "Sean" <seanlkml@roger s.com> wrote...[color=blue]
    > #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?[/color]


    Try

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

    Victor


    Comment

    • Sean

      #3
      Re: stl for_each makes two additional copies of functor


      "Howard Hinnant" <hinnant@metrow erks.com> wrote in message
      news:3006200319 12177840%hinnan t@metrowerks.co m...
      <snip>[color=blue]
      >
      > 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<i nt*, 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/wg2...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[/color]

      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


      Comment

      Working...