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

--- Comment #12 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <r...@gcc.gnu.org>:

https://gcc.gnu.org/g:64c986b49558a7c356b85bda85195216936c29a3

commit r13-4752-g64c986b49558a7c356b85bda85195216936c29a3
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Wed Dec 14 15:21:32 2022 +0000

    libstdc++: Diagnose broken allocator rebind members

    This adds a static assertion to std::allocator_traits::rebind_alloc to
    diagnose violations of the rule that rebinding an allocator to its own
    value type yields the same allocator type.

    This helps to catch the easy mistake of deriving from std::allocator but
    forgetting to override the rebind behaviour (no longer an issue in C++20
    as std::allocator doesn't have a rebind member that can be inherited).
    It also catches bugs like in 23_containers/vector/52591.cc where a typo
    means the rebound allocator is a completely different type.

    I initially wanted to put this static assert into the body of
    allocator_traits:

          static_assert(is_same<rebind_alloc<value_type>, _Alloc>::value,
                        "rebind_alloc<value_type> must be Alloc");

    However, this causes a regression in the test for PR libstdc++/72792.
    It seems that instantiating std::allocator_traits should be allowed for
    invalid allocator types as long as you don't try to rebind them. To
    support that, only assert in the __allocator_traits_base::__rebind class
    template (in both the primary template and the partial specialization).
    As a result, the bug in 20_util/scoped_allocator/outermost.cc is not
    diagnosed, because nothing in that test rebinds the allocator.

    libstdc++-v3/ChangeLog:

            * include/bits/alloc_traits.h (__allocator_traits_base::__rebind):
            Add static assert for rebind requirement.
            * testsuite/20_util/allocator_traits/members/rebind_alloc.cc:
            Fix invalid rebind member in test allocator.
            * testsuite/20_util/allocator_traits/requirements/rebind_neg.cc:
            New test.
            * testsuite/20_util/scoped_allocator/outermost.cc: Add rebind to
            test allocator.
            * testsuite/23_containers/forward_list/48101_neg.cc: Prune new
            static assert error.
            * testsuite/23_containers/unordered_multiset/48101_neg.cc:
            Likewise.
            * testsuite/23_containers/unordered_set/48101_neg.cc:
            Likewise.
            * testsuite/23_containers/vector/52591.cc: Fix typo in rebind.

Reply via email to