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

            Bug ID: 120432
           Summary: flat_map operator[] is broken for const lvalue keys
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arthur.j.odwyer at gmail dot com
  Target Milestone: ---

As reported by Rufei Zhao on the cpplang Slack: https://godbolt.org/z/fa48dvnoq

    std::flat_map<double, double> m;
    int main(void) {
      const double e { 3.14 };
      std::flat_map<double, double> m; 
      m[e] = 1.0; // bogus error
    }

The problem is that
https://github.com/gcc-mirror/gcc/blob/aa93272/libstdc%2B%2B-v3/include/std/flat_map#L1145
says:

      mapped_type&
      operator[](const key_type& __x)
      { return operator[]<const key_type>(__x); }

when it means:

      mapped_type&
      operator[](const key_type& __x)
      { return operator[]<const key_type&>(__x); }

I'd strongly recommend that you use a different name than `operator[]<U>` for
the helper function anyway, because I'm sure there will be additional problems
around that that I'm not seeing off the top of my head. ...Actually, I see
another easy one off the top of my head.
https://godbolt.org/z/ea4WGcbz3

    template<class M, class T>
      concept C = requires (M& m, T t) { m[t]; };

    static_assert(!C<std::map<int, int>, volatile int>);
    static_assert(!C<std::unordered_map<int, int>, volatile int>);
    static_assert(!C<std::flat_map<int, int>, volatile int>); // bogus error

You need that helper function to stay out of the overload set of operator[] so
that it doesn't contribute to overload resolution in cases like this.

Reply via email to