On Tue, May 27, 2025 at 7:08 PM Patrick Palka <ppa...@redhat.com> wrote:

> Tested on x86_64-pc-linux-gnu, does this look OK for trunk/15?
>
> The 'volatile' issue from that PR Will be fixed in a separate patch as
> operator[] isn't the only operation that's affected.
>
> -- >8 --
>
> The const lvalue operator[] overload wasn't properly forwarding the key
> type to the generic overload.
>
>         PR libstdc++/120432
>
> libstdc++-v3/ChangeLog:
>
>         * include/std/flat_map (_Flat_map_base::operator[]): Correct
>         forwarding from the const lvalue key overload.
>         * testsuite/23_containers/flat_map/1.cc (test08): New test.
>         * testsuite/23_containers/flat_multimap/1.cc (test08): New test.
> ---
>  libstdc++-v3/include/std/flat_map                      |  2 +-
>  libstdc++-v3/testsuite/23_containers/flat_map/1.cc     | 10 ++++++++++
>  .../testsuite/23_containers/flat_multimap/1.cc         | 10 ++++++++++
>  3 files changed, 21 insertions(+), 1 deletion(-)
>
> diff --git a/libstdc++-v3/include/std/flat_map
> b/libstdc++-v3/include/std/flat_map
> index 6593988d213c..4d9ced1e8191 100644
> --- a/libstdc++-v3/include/std/flat_map
> +++ b/libstdc++-v3/include/std/flat_map
> @@ -1142,7 +1142,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>        // element access
>        mapped_type&
>        operator[](const key_type& __x)
> -      { return operator[]<const key_type>(__x); }
> +      { return operator[]<const key_type&>(__x); }
>
Given that the operator[] that we are forading to is implemented as:
        { return try_emplace(std::forward<_Key2>(__x)).first->second; }
I would just call try_emplace directly:

>
>        mapped_type&
>        operator[](key_type&& __x)
> diff --git a/libstdc++-v3/testsuite/23_containers/flat_map/1.cc
> b/libstdc++-v3/testsuite/23_containers/flat_map/1.cc
> index a9690208b09f..2af516410279 100644
> --- a/libstdc++-v3/testsuite/23_containers/flat_map/1.cc
> +++ b/libstdc++-v3/testsuite/23_containers/flat_map/1.cc
> @@ -253,6 +253,15 @@ test07()
>    VERIFY( std::ranges::equal(m, (std::pair<int,int>[]){{3,4}}) );
>  }
>
> +void
> +test08()
> +{
> +  // PR libstdc++/120432 - flat_map operator[] is broken for const lvalue
> keys
> +  std::flat_map<int, int> m;
> +  const int k = 42;
> +  m[k] = 0;
> +}
> +
>  int
>  main()
>  {
> @@ -266,4 +275,5 @@ main()
>    test05();
>    test06();
>    test07();
> +  test08();
>  }
> diff --git a/libstdc++-v3/testsuite/23_containers/flat_multimap/1.cc
> b/libstdc++-v3/testsuite/23_containers/flat_multimap/1.cc
> index 1c5c9a88ab6b..638b269011d4 100644
> --- a/libstdc++-v3/testsuite/23_containers/flat_multimap/1.cc
> +++ b/libstdc++-v3/testsuite/23_containers/flat_multimap/1.cc
> @@ -231,6 +231,15 @@ test07()
>    VERIFY( std::ranges::equal(m, (std::pair<int,int>[]){{3,4},{3,3}}) );
>  }
>
> +void
> +test08()
> +{
> +  // PR libstdc++/120432 - flat_map operator[] is broken for const lvalue
> keys
> +  std::flat_multimap<int, int> m;
> +  const int k = 42;
> +  m[k] = 0;
> +}
> +
>  int
>  main()
>  {
> @@ -244,4 +253,5 @@ main()
>    test05();
>    test06();
>    test07();
> +  test08();
>  }
> --
> 2.49.0.654.g845c48a16a
>
>

Reply via email to