From 738294afb1d87615b6c9debac252327d6dec13cb Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Tue, 30 May 2017 23:38:15 -0700 Subject: [PATCH] working on util::variant --- src/util/variant.h | 109 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 87 insertions(+), 22 deletions(-) diff --git a/src/util/variant.h b/src/util/variant.h index e2bf17d..8a0d03d 100644 --- a/src/util/variant.h +++ b/src/util/variant.h @@ -173,23 +173,6 @@ struct variant_hypothetical_overload_set static void fn(); // not implemented }; -template -struct variant_conversion_deduce_type_helper; - -template -struct variant_conversion_deduce_type_helper::fn( - std::declval()))::type>> -{ - typedef typename decltype( - variant_hypothetical_overload_set::fn(std::declval()))::type type; -}; - -template -using variant_conversion_deduce_type = - typename variant_conversion_deduce_type_helper::type; - template union variant_values { @@ -288,6 +271,20 @@ union variant_values : other_values(in_place_index, std::forward(args)...) { } + template < + typename U, + typename... Args, + typename = typename std:: + enable_if, Args...>::value>::type> + constexpr variant_values( + in_place_index_t<0>, + std::initializer_list il, + Args &&... args) noexcept(std::is_nothrow_constructible, + Args...>::value) + : current_value(il, std::forward(args)...) + { + } template static constexpr std::size_t index_from_type() noexcept { @@ -338,6 +335,21 @@ union variant_values } }; +template ::fn(std::declval()))::type, + std::size_t Index = variant_values::index_from_type(), + typename = typename std::enable_if<(Index < sizeof...(Types))>::type> +constexpr std::size_t variant_conversion_deduce_index() noexcept +{ + return Index; +} + +template +using variant_conversion_deduce_type = + variant_alternative_t(), Types...>; + template struct variant_index_type { @@ -416,6 +428,30 @@ struct variant_base } ~variant_base() = default; }; + +template +struct variant_is_in_place_index +{ + static constexpr bool value = false; +}; + +template +struct variant_is_in_place_index> +{ + static constexpr bool value = true; +}; + +template +struct variant_is_in_place_type +{ + static constexpr bool value = false; +}; + +template +struct variant_is_in_place_type> +{ + static constexpr bool value = true; +}; } template @@ -460,15 +496,44 @@ public: values.move_construct(std::move(rt.values), rt.index_value.get()); index_value = rt.index_value; } - template (), + typename = typename std:: + enable_if::type, variant>::value + && !detail::variant_is_in_place_index::type>::value + && !detail::variant_is_in_place_type::type>::value>:: + type> + constexpr variant(T &&value) noexcept( + std::is_nothrow_constructible>, T>::value) + : base(Index, in_place_index, std::forward(value)) + { + } + template >, + enable_if>, Args...>::value>::type> - constexpr explicit variant(in_place_index_t, Args &&... args) noexcept( - std::is_nothrow_constructible>, + constexpr explicit variant(in_place_index_t, Args &&... args) noexcept( + std::is_nothrow_constructible>, Args...>::value) - : base(index, in_place_index, std::forward(args)...) + : base(Index, in_place_index, std::forward(args)...) + { + } + template >, + std::initializer_list, + Args...>::value>::type> + constexpr explicit variant(in_place_index_t, + std::initializer_list il, + Args &&... args) // + noexcept(std::is_nothrow_constructible>, + std::initializer_list, + Args...>::value) + : base(Index, in_place_index, il, std::forward(args)...) { } #error finish -- 2.30.2