1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 
12 // <variant>
13 
14 // template <class ...Types> class variant;
15 
16 #include <limits>
17 #include <type_traits>
18 #include <utility>
19 #include <variant>
20 
21 #include "test_macros.h"
22 
23 template <class Sequence>
24 struct make_variant_imp;
25 
26 template <size_t ...Indices>
27 struct make_variant_imp<std::integer_sequence<size_t, Indices...>> {
28   template <size_t> using AlwaysChar = char;
29   using type = std::variant<AlwaysChar<Indices>...>;
30 };
31 
32 template <size_t N>
33 using make_variant_t = typename make_variant_imp<std::make_index_sequence<N>>::type;
34 
35 constexpr bool ExpectEqual =
36 #ifdef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
37   false;
38 #else
39   true;
40 #endif
41 
42 template <class IndexType>
test_index_type()43 void test_index_type() {
44   using Lim = std::numeric_limits<IndexType>;
45   using T1 = make_variant_t<Lim::max() - 1>;
46   using T2 = make_variant_t<Lim::max()>;
47   static_assert((sizeof(T1) == sizeof(T2)) == ExpectEqual, "");
48 }
49 
50 template <class IndexType>
test_index_internals()51 void test_index_internals() {
52   using Lim = std::numeric_limits<IndexType>;
53   static_assert(std::__choose_index_type(Lim::max() -1) !=
54                 std::__choose_index_type(Lim::max()), "");
55   static_assert(std::is_same_v<
56       std::__variant_index_t<Lim::max()-1>,
57       std::__variant_index_t<Lim::max()>
58     > == ExpectEqual, "");
59   using IndexT = std::__variant_index_t<Lim::max()-1>;
60   using IndexLim = std::numeric_limits<IndexT>;
61   static_assert(std::__variant_npos<IndexT> == IndexLim::max(), "");
62 }
63 
main(int,char **)64 int main(int, char**) {
65   test_index_type<unsigned char>();
66   // This won't compile due to template depth issues.
67   //test_index_type<unsigned short>();
68   test_index_internals<unsigned char>();
69   test_index_internals<unsigned short>();
70 
71   return 0;
72 }
73