From 03e4501ff4ea635507f91f4e2e95f2b7448e2e67 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 29 Sep 2026 23:13:49 +0200 Subject: [PATCH] Declare the literal operators through a local macro The GCC 4.8 spacing condition was repeated for both operator definitions and the global using-declarations. NLOHMANN_JSON_LITERAL_OPERATOR(suffix) now selects operator""##suffix or operator"" suffix in one place and is undefined at the end of json_literals.hpp. Suggested by gregmarr in review. Signed-off-by: Niels Lohmann --- include/nlohmann/json_literals.hpp | 34 ++++++++++------------- single_include/nlohmann/json.hpp | 34 ++++++++++------------- single_include/nlohmann/json_literals.hpp | 34 ++++++++++------------- 3 files changed, 42 insertions(+), 60 deletions(-) diff --git a/include/nlohmann/json_literals.hpp b/include/nlohmann/json_literals.hpp index f304039e4..a865b6c82 100644 --- a/include/nlohmann/json_literals.hpp +++ b/include/nlohmann/json_literals.hpp @@ -21,6 +21,14 @@ // amalgamation inlines macro_scope.hpp only once), so only standard and public // macros may be used below. +// declares the literal operator for the given suffix; GCC 4.8 requires a space +// between "" and the suffix, which newer compilers deprecate (CWG 2521) +#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) + #define NLOHMANN_JSON_LITERAL_OPERATOR(suffix) operator""##suffix +#else + #define NLOHMANN_JSON_LITERAL_OPERATOR(suffix) operator"" suffix +#endif + NLOHMANN_JSON_NAMESPACE_BEGIN inline namespace literals { @@ -29,12 +37,7 @@ inline namespace json_literals /// @brief user-defined string literal for JSON values /// @sa https://json.nlohmann.me/api/operator_literal_json/ -#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) - inline nlohmann::json operator""_json(const char* s, std::size_t n) -#else - // GCC 4.8 requires a space between "" and suffix - inline nlohmann::json operator"" _json(const char* s, std::size_t n) -#endif +inline nlohmann::json NLOHMANN_JSON_LITERAL_OPERATOR(_json)(const char* s, std::size_t n) { return nlohmann::json::parse(s, s + n); } @@ -49,12 +52,7 @@ inline nlohmann::json operator""_json(const char8_t* s, std::size_t n) /// @brief user-defined string literal for JSON pointer /// @sa https://json.nlohmann.me/api/operator_literal_json_pointer/ -#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) - inline nlohmann::json::json_pointer operator""_json_pointer(const char* s, std::size_t n) -#else - // GCC 4.8 requires a space between "" and suffix - inline nlohmann::json::json_pointer operator"" _json_pointer(const char* s, std::size_t n) -#endif +inline nlohmann::json::json_pointer NLOHMANN_JSON_LITERAL_OPERATOR(_json_pointer)(const char* s, std::size_t n) { return nlohmann::json::json_pointer(std::string(s, n)); } @@ -71,14 +69,10 @@ inline nlohmann::json::json_pointer operator""_json_pointer(const char8_t* s, st NLOHMANN_JSON_NAMESPACE_END #if !defined(JSON_USE_GLOBAL_UDLS) || JSON_USE_GLOBAL_UDLS - #if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) - using nlohmann::literals::json_literals::operator""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers) - using nlohmann::literals::json_literals::operator""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers) - #else - // GCC 4.8 requires a space between "" and suffix - using nlohmann::literals::json_literals::operator"" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers) - using nlohmann::literals::json_literals::operator"" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers) - #endif + using nlohmann::literals::json_literals::NLOHMANN_JSON_LITERAL_OPERATOR(_json); // NOLINT(misc-unused-using-decls,google-global-names-in-headers) + using nlohmann::literals::json_literals::NLOHMANN_JSON_LITERAL_OPERATOR(_json_pointer); //NOLINT(misc-unused-using-decls,google-global-names-in-headers) #endif +#undef NLOHMANN_JSON_LITERAL_OPERATOR + #endif // INCLUDE_NLOHMANN_JSON_LITERALS_HPP_ diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index d32e8ed1e..73c5d00c1 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -32885,6 +32885,14 @@ struct formatter // NOLINT(cert-dcl58-c // amalgamation inlines macro_scope.hpp only once), so only standard and public // macros may be used below. +// declares the literal operator for the given suffix; GCC 4.8 requires a space +// between "" and the suffix, which newer compilers deprecate (CWG 2521) +#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) + #define NLOHMANN_JSON_LITERAL_OPERATOR(suffix) operator""##suffix +#else + #define NLOHMANN_JSON_LITERAL_OPERATOR(suffix) operator"" suffix +#endif + NLOHMANN_JSON_NAMESPACE_BEGIN inline namespace literals { @@ -32893,12 +32901,7 @@ inline namespace json_literals /// @brief user-defined string literal for JSON values /// @sa https://json.nlohmann.me/api/operator_literal_json/ -#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) - inline nlohmann::json operator""_json(const char* s, std::size_t n) -#else - // GCC 4.8 requires a space between "" and suffix - inline nlohmann::json operator"" _json(const char* s, std::size_t n) -#endif +inline nlohmann::json NLOHMANN_JSON_LITERAL_OPERATOR(_json)(const char* s, std::size_t n) { return nlohmann::json::parse(s, s + n); } @@ -32913,12 +32916,7 @@ inline nlohmann::json operator""_json(const char8_t* s, std::size_t n) /// @brief user-defined string literal for JSON pointer /// @sa https://json.nlohmann.me/api/operator_literal_json_pointer/ -#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) - inline nlohmann::json::json_pointer operator""_json_pointer(const char* s, std::size_t n) -#else - // GCC 4.8 requires a space between "" and suffix - inline nlohmann::json::json_pointer operator"" _json_pointer(const char* s, std::size_t n) -#endif +inline nlohmann::json::json_pointer NLOHMANN_JSON_LITERAL_OPERATOR(_json_pointer)(const char* s, std::size_t n) { return nlohmann::json::json_pointer(std::string(s, n)); } @@ -32935,16 +32933,12 @@ inline nlohmann::json::json_pointer operator""_json_pointer(const char8_t* s, st NLOHMANN_JSON_NAMESPACE_END #if !defined(JSON_USE_GLOBAL_UDLS) || JSON_USE_GLOBAL_UDLS - #if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) - using nlohmann::literals::json_literals::operator""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers) - using nlohmann::literals::json_literals::operator""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers) - #else - // GCC 4.8 requires a space between "" and suffix - using nlohmann::literals::json_literals::operator"" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers) - using nlohmann::literals::json_literals::operator"" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers) - #endif + using nlohmann::literals::json_literals::NLOHMANN_JSON_LITERAL_OPERATOR(_json); // NOLINT(misc-unused-using-decls,google-global-names-in-headers) + using nlohmann::literals::json_literals::NLOHMANN_JSON_LITERAL_OPERATOR(_json_pointer); //NOLINT(misc-unused-using-decls,google-global-names-in-headers) #endif +#undef NLOHMANN_JSON_LITERAL_OPERATOR + #endif // INCLUDE_NLOHMANN_JSON_LITERALS_HPP_ #endif diff --git a/single_include/nlohmann/json_literals.hpp b/single_include/nlohmann/json_literals.hpp index f304039e4..a865b6c82 100644 --- a/single_include/nlohmann/json_literals.hpp +++ b/single_include/nlohmann/json_literals.hpp @@ -21,6 +21,14 @@ // amalgamation inlines macro_scope.hpp only once), so only standard and public // macros may be used below. +// declares the literal operator for the given suffix; GCC 4.8 requires a space +// between "" and the suffix, which newer compilers deprecate (CWG 2521) +#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) + #define NLOHMANN_JSON_LITERAL_OPERATOR(suffix) operator""##suffix +#else + #define NLOHMANN_JSON_LITERAL_OPERATOR(suffix) operator"" suffix +#endif + NLOHMANN_JSON_NAMESPACE_BEGIN inline namespace literals { @@ -29,12 +37,7 @@ inline namespace json_literals /// @brief user-defined string literal for JSON values /// @sa https://json.nlohmann.me/api/operator_literal_json/ -#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) - inline nlohmann::json operator""_json(const char* s, std::size_t n) -#else - // GCC 4.8 requires a space between "" and suffix - inline nlohmann::json operator"" _json(const char* s, std::size_t n) -#endif +inline nlohmann::json NLOHMANN_JSON_LITERAL_OPERATOR(_json)(const char* s, std::size_t n) { return nlohmann::json::parse(s, s + n); } @@ -49,12 +52,7 @@ inline nlohmann::json operator""_json(const char8_t* s, std::size_t n) /// @brief user-defined string literal for JSON pointer /// @sa https://json.nlohmann.me/api/operator_literal_json_pointer/ -#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) - inline nlohmann::json::json_pointer operator""_json_pointer(const char* s, std::size_t n) -#else - // GCC 4.8 requires a space between "" and suffix - inline nlohmann::json::json_pointer operator"" _json_pointer(const char* s, std::size_t n) -#endif +inline nlohmann::json::json_pointer NLOHMANN_JSON_LITERAL_OPERATOR(_json_pointer)(const char* s, std::size_t n) { return nlohmann::json::json_pointer(std::string(s, n)); } @@ -71,14 +69,10 @@ inline nlohmann::json::json_pointer operator""_json_pointer(const char8_t* s, st NLOHMANN_JSON_NAMESPACE_END #if !defined(JSON_USE_GLOBAL_UDLS) || JSON_USE_GLOBAL_UDLS - #if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) - using nlohmann::literals::json_literals::operator""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers) - using nlohmann::literals::json_literals::operator""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers) - #else - // GCC 4.8 requires a space between "" and suffix - using nlohmann::literals::json_literals::operator"" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers) - using nlohmann::literals::json_literals::operator"" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers) - #endif + using nlohmann::literals::json_literals::NLOHMANN_JSON_LITERAL_OPERATOR(_json); // NOLINT(misc-unused-using-decls,google-global-names-in-headers) + using nlohmann::literals::json_literals::NLOHMANN_JSON_LITERAL_OPERATOR(_json_pointer); //NOLINT(misc-unused-using-decls,google-global-names-in-headers) #endif +#undef NLOHMANN_JSON_LITERAL_OPERATOR + #endif // INCLUDE_NLOHMANN_JSON_LITERALS_HPP_