diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index e4551d1ba..cadc6438e 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -13,6 +13,9 @@ #include // tuple #include // false_type, is_constructible, is_integral, is_same, true_type #include // declval +#if defined(__cpp_lib_optional) && __cpp_lib_optional >= 201606L + #include // optional +#endif #if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L #include // byte #endif @@ -77,6 +80,15 @@ struct is_json_ref : std::false_type {}; template struct is_json_ref> : std::true_type {}; +// trait to detect std::optional specializations +template +struct is_std_optional : std::false_type {}; + +#if defined(__cpp_lib_optional) && __cpp_lib_optional >= 201606L +template +struct is_std_optional> : std::true_type {}; +#endif + ////////////////////////// // aliases for detected // ////////////////////////// diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 5e44e8d70..c9de854c5 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1937,6 +1937,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #endif #if defined(JSON_HAS_CPP_17) && JSON_HAS_STATIC_RTTI detail::negation>, +#endif +#if defined(JSON_HAS_CPP_17) + // std::optional can construct itself from basic_json; excluding it + // here avoids an ambiguity with that constructor (e.g., under C++26) + detail::negation>, #endif detail::is_detected_lazy >::value, int >::type = 0 > diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2facfbdf8..98725173f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3570,6 +3570,9 @@ NLOHMANN_JSON_NAMESPACE_END #include // tuple #include // false_type, is_constructible, is_integral, is_same, true_type #include // declval +#if defined(__cpp_lib_optional) && __cpp_lib_optional >= 201606L + #include // optional +#endif #if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L #include // byte #endif @@ -3817,6 +3820,15 @@ struct is_json_ref : std::false_type {}; template struct is_json_ref> : std::true_type {}; +// trait to detect std::optional specializations +template +struct is_std_optional : std::false_type {}; + +#if defined(__cpp_lib_optional) && __cpp_lib_optional >= 201606L +template +struct is_std_optional> : std::true_type {}; +#endif + ////////////////////////// // aliases for detected // ////////////////////////// @@ -22446,6 +22458,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #endif #if defined(JSON_HAS_CPP_17) && JSON_HAS_STATIC_RTTI detail::negation>, +#endif +#if defined(JSON_HAS_CPP_17) + // std::optional can construct itself from basic_json; excluding it + // here avoids an ambiguity with that constructor (e.g., under C++26) + detail::negation>, #endif detail::is_detected_lazy >::value, int >::type = 0 >