From e26322110a55708a97ad00da8da70a90dc2da6f7 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Sat, 20 Apr 2024 08:59:03 +0800 Subject: [PATCH] add restrict for variant --- iguana/json_reader.hpp | 6 ++---- iguana/json_writer.hpp | 2 ++ iguana/util.hpp | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/iguana/json_reader.hpp b/iguana/json_reader.hpp index 1dcd9a58..60fca2a6 100644 --- a/iguana/json_reader.hpp +++ b/iguana/json_reader.hpp @@ -512,13 +512,11 @@ IGUANA_INLINE bool from_json_variant_impl(U &value, It it, It end, It &temp_it, } } -template -using variant_element_t = std::remove_reference_t( - std::declval>()))>; - template IGUANA_INLINE void from_json_variant(U &value, It &it, It &end, std::index_sequence) { + static_assert(!has_same_variant_type_v>, + "don't allow same type in std::variant"); bool r = false; It temp_it = it; It temp_end = end; diff --git a/iguana/json_writer.hpp b/iguana/json_writer.hpp index 80e2558d..9f6f7360 100644 --- a/iguana/json_writer.hpp +++ b/iguana/json_writer.hpp @@ -245,6 +245,8 @@ IGUANA_INLINE void to_json_impl(Stream &s, T &&t) { template , int>> IGUANA_INLINE void to_json_impl(Stream &s, T &&t) { + static_assert(!has_same_variant_type_v>, + "don't allow same type in std::variant"); std::visit( [&s](auto value) { to_json_impl(s, value); diff --git a/iguana/util.hpp b/iguana/util.hpp index f910db79..5d622712 100644 --- a/iguana/util.hpp +++ b/iguana/util.hpp @@ -139,6 +139,20 @@ struct is_variant> : std::true_type {}; template constexpr inline bool variant_v = is_variant>::value; +template +struct has_same_variant_type : std::false_type {}; + +template +struct has_same_variant_type> + : std::disjunction...> {}; + +template +constexpr bool has_same_variant_type_v = has_same_variant_type::value; + +template +using variant_element_t = std::remove_reference_t( + std::declval>()))>; + template constexpr inline bool refletable_v = is_reflection_v>;