1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-31 10:24:23 +03:00

Replace deprecated std::is_trivial in C++26 (#4775)

This commit is contained in:
NmPassTHFan
2025-05-16 22:11:44 +08:00
committed by GitHub
parent 6f6be39332
commit 46e7cd3dc2
4 changed files with 46 additions and 6 deletions

View File

@ -2395,8 +2395,14 @@ JSON_HEDLEY_DIAGNOSTIC_POP
// C++ language standard detection
// if the user manually specified the used C++ version, this is skipped
#if !defined(JSON_HAS_CPP_23) && !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11)
#if (defined(__cplusplus) && __cplusplus > 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG > 202002L)
#if !defined(JSON_HAS_CPP_26) && !defined(JSON_HAS_CPP_23) && !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11)
#if (defined(__cplusplus) && __cplusplus > 202302L) || (defined(_MSVC_LANG) && _MSVC_LANG > 202302L)
#define JSON_HAS_CPP_26
#define JSON_HAS_CPP_23
#define JSON_HAS_CPP_20
#define JSON_HAS_CPP_17
#define JSON_HAS_CPP_14
#elif (defined(__cplusplus) && __cplusplus > 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG > 202002L)
#define JSON_HAS_CPP_23
#define JSON_HAS_CPP_20
#define JSON_HAS_CPP_17
@ -17605,8 +17611,21 @@ class binary_writer
enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
static CharType to_char_type(std::uint8_t x) noexcept
{
static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
// The std::is_trivial trait is deprecated in C++26. The replacement is to use
// std::is_trivially_copyable and std::is_trivially_default_constructible.
// However, some older library implementations support std::is_trivial
// but not all the std::is_trivially_* traits.
// Since detecting full support across all libraries is difficult,
// we use std::is_trivial unless we are using a standard where it has been deprecated.
// For more details, see: https://github.com/nlohmann/json/pull/4775#issuecomment-2884361627
#ifdef JSON_HAS_CPP_26
static_assert(std::is_trivially_copyable<CharType>::value, "CharType must be trivially copyable");
static_assert(std::is_trivially_default_constructible<CharType>::value, "CharType must be trivially default constructible");
#else
static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
#endif
static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
CharType result;
std::memcpy(&result, &x, sizeof(x));
return result;
@ -25407,6 +25426,7 @@ inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC
#undef JSON_HAS_CPP_17
#undef JSON_HAS_CPP_20
#undef JSON_HAS_CPP_23
#undef JSON_HAS_CPP_26
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#undef JSON_HAS_THREE_WAY_COMPARISON