The program below used to print "5".  With g++ 4.3, it prints "0".
The program defines an output iterator which stores a counter,
to be incremented on each assignment of the output iterator.

The problem is that std::copy now copies the output iterator before
performing the assignment, so the program does not work anymore.

I am not sure it is a bug, I think it isn't.
Nevertheless, in doubt, I wanted to report the issue, as I think copying
output iterators too many times can also be a source of performance
degradation in some cases.

----------------------------

#include <algorithm>
#include <iostream>

class Counting_output_iterator
  : public std::iterator< std::output_iterator_tag, void, void, void, void >
{
    std::size_t c;
public:
    Counting_output_iterator() : c(0) {}
    Counting_output_iterator& operator++() { return *this; }
    Counting_output_iterator& operator*() { return *this; }

    template <typename T>
    void operator=(const T&) { ++c; }

    std::size_t current_counter() const { return c; }
};

int main()
{
        int t[10] = {0,};
        Counting_output_iterator cnt;
        std::size_t res = std::copy(t+0, t+5, cnt).current_counter();
        std::cout << res << std::endl;
}


-- 
           Summary: std::copy copies the output iterator before assigning
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sylvain dot pion at sophia dot inria dot fr
  GCC host triplet: i386-apple-darwin8.11.1


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34595

Reply via email to