https://github.com/nlohmann/json/issues/3927 https://github.com/nlohmann/json/pull/3895 From a5b09d50b786638ed9deb09ef13860a3cb64eb6b Mon Sep 17 00:00:00 2001 From: Sergei Trofimovich Date: Tue, 20 Dec 2022 22:08:12 +0000 Subject: [PATCH] custom allocators: define missing 'rebind' type `gcc-13` added an assert to standard headers to make sure custom allocators have intended implementation of rebind type instead of inherited rebind. gcc change: https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=64c986b49558a7 Without the fix build fails on this week's `gcc-13` as: In file included from <>-gcc-13.0.0/include/c++/13.0.0/ext/alloc_traits.h:34, from <>-gcc-13.0.0/include/c++/13.0.0/bits/basic_string.h:39, from <>-gcc-13.0.0/include/c++/13.0.0/string:54, from <>-gcc-13.0.0/include/c++/13.0.0/bits/locale_classes.h:40, from <>-gcc-13.0.0/include/c++/13.0.0/locale:41, from tests/src/unit-regression2.cpp:19: <>-gcc-13.0.0/include/c++/13.0.0/bits/alloc_traits.h: In instantiation of 'struct std::__allocator_traits_base::__rebind, unsigned char, void>': <>-gcc-13.0.0/include/c++/13.0.0/bits/alloc_traits.h:94:11: required by substitution of 'template using std::__alloc_rebind = typename std::__allocator_traits_base::__rebind<_Alloc, _Up>::type [with _Alloc = my_allocator; _Up = unsigned char]' <>-gcc-13.0.0/include/c++/13.0.0/bits/alloc_traits.h:228:8: required by substitution of 'template template using std::allocator_traits< >::rebind_alloc = std::__alloc_rebind<_Alloc, _Tp> [with _Tp = unsigned char; _Alloc = my_allocator]' <>-gcc-13.0.0/include/c++/13.0.0/ext/alloc_traits.h:126:65: required from 'struct __gnu_cxx::__alloc_traits, unsigned char>::rebind' <>-gcc-13.0.0/include/c++/13.0.0/bits/stl_vector.h:88:21: required from 'struct std::_Vector_base >' <>-gcc-13.0.0/include/c++/13.0.0/bits/stl_vector.h:423:11: required from 'class std::vector >' tests/src/unit-regression2.cpp:807:63: required from here <>-gcc-13.0.0/include/c++/13.0.0/bits/alloc_traits.h:70:31: error: static assertion failed: allocator_traits::rebind_alloc must be A 70 | _Tp>::value, | ^~~~~ The change adds trivial `rebind` definition with expected return type and satisfies conversion requirements. --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -20,11 +20,20 @@ struct bad_allocator : std::allocator { using std::allocator::allocator; + bad_allocator() = default; + template bad_allocator(const bad_allocator& /*unused*/) { } + template void construct(T* /*unused*/, Args&& ... /*unused*/) { throw std::bad_alloc(); } + + template + struct rebind + { + using other = bad_allocator; + }; }; } // namespace --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -187,6 +187,15 @@ class my_allocator : public std::allocator { public: using std::allocator::allocator; + + my_allocator() = default; + template my_allocator(const my_allocator& /*unused*/) { } + + template + struct rebind + { + using other = my_allocator; + }; }; /////////////////////////////////////////////////////////////////////