• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 // UNSUPPORTED: c++98, c++03, c++11, c++14
12 
13 // <variant>
14 
15 // template <class ...Types> class variant;
16 
17 #include <limits>
18 #include <type_traits>
19 #include <utility>
20 #include <variant>
21 
22 template <class Sequence>
23 struct make_variant_imp;
24 
25 template <size_t ...Indices>
26 struct make_variant_imp<std::integer_sequence<size_t, Indices...>> {
27   using type = std::variant<decltype((Indices, char(0)))...>;
28 };
29 
30 template <size_t N>
31 using make_variant_t = typename make_variant_imp<std::make_index_sequence<N>>::type;
32 
33 constexpr bool ExpectEqual =
34 #ifdef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
35   false;
36 #else
37   true;
38 #endif
39 
40 template <class IndexType>
test_index_type()41 void test_index_type() {
42   using Lim = std::numeric_limits<IndexType>;
43   using T1 = make_variant_t<Lim::max() - 1>;
44   using T2 = make_variant_t<Lim::max()>;
45   static_assert((sizeof(T1) == sizeof(T2)) == ExpectEqual, "");
46 }
47 
48 template <class IndexType>
test_index_internals()49 void test_index_internals() {
50   using Lim = std::numeric_limits<IndexType>;
51   static_assert(std::__choose_index_type(Lim::max() -1) !=
52                 std::__choose_index_type(Lim::max()), "");
53   static_assert(std::is_same_v<
54       std::__variant_index_t<Lim::max()-1>,
55       std::__variant_index_t<Lim::max()>
56     > == ExpectEqual, "");
57   using IndexT = std::__variant_index_t<Lim::max()-1>;
58   using IndexLim = std::numeric_limits<IndexT>;
59   static_assert(std::__variant_npos<IndexT> == IndexLim::max(), "");
60 }
61 
main()62 int main() {
63   test_index_type<unsigned char>();
64   // This won't compile due to template depth issues.
65   //test_index_type<unsigned short>();
66   test_index_internals<unsigned char>();
67   test_index_internals<unsigned short>();
68 }
69