https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89065

            Bug ID: 89065
           Summary: set::find always returns const iterator
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 1000hz.radiowave at gmail dot com
  Target Milestone: ---

by the standard there are 2 overloads of find:
iterator find( const Key& key );
const_iterator find( const Key& key ) const;

However, libstdc++ seem to return const iterator even on non const object.
Consider the example:

#include <iostream>
#include <set>
struct FatKey   { int x; int ref_count; };
bool operator<(const FatKey& fk1, const FatKey& fk2) { return fk1.x < fk2.x; }
int main()
{  
    std::set<FatKey> example = { {1, 0 }, {2, 0 }, {3, 0 }, {4, 0 } };

    auto search = example.find({1, 0});
    if (search != example.end()) {
        search->ref_count++;
        std::cout << "Found " << search->x << '\n';
    } else {
        std::cout << "Not found\n";
    }
}


It fails to compile with the error:

main.cpp: In function 'int main()':
main.cpp:11:26: error: increment of member 'FatKey::ref_count' in read-only
object
         search->ref_count++;

While it shouldn't.

Reply via email to