Tests: guard sweep_allocator::construct() against const types instead of const_cast

The const_cast added in the previous commit is rejected by
cppcoreguidelines-pro-type-const-cast (clang-tidy). Make construct() not
viable for const U instead, which is what libc++ probes for.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N5NasEsrzHhSNouxSoEwqz
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels LohmannandClaude Sonnet 5.5 committed 2026-11-07 08:33:16 +00:00
1 parent f0cbfa5bb0
commit ee378e1e75
1 file changed
+6 -5
+6 -5
View File
@@ -14,6 +14,7 @@ using nlohmann::json;
#include <functional>
#include <limits>
#include <type_traits>
#include <valarray>
#if JSON_HAS_RANGES
#include <ranges>
@@ -1073,12 +1074,12 @@ struct sweep_allocator
return (std::numeric_limits<std::size_t>::max)() / sizeof(T);
}
// p may point to a const object when libc++ probes whether construct() is
// usable for copying, which static_cast<void*>(p) would not accept
template<class U, class... Args>
void construct(U* p, Args&& ... args) noexcept(noexcept(::new (const_cast<void*>(static_cast<const volatile void*>(p))) U(std::forward<Args>(args)...)))
// libc++ probes whether construct() is usable for const objects (when
// checking for trivial copies); this must not be viable then
template<class U, class... Args, typename = typename std::enable_if<!std::is_const<U>::value>::type>
void construct(U* p, Args&& ... args) noexcept(noexcept(::new (static_cast<void*>(p)) U(std::forward<Args>(args)...)))
{
::new (const_cast<void*>(static_cast<const volatile void*>(p))) U(std::forward<Args>(args)...);
::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
}
template<class U>