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++03, c++11, c++14
11
12 // GCC 5 pretends it supports C++17, but it doesn't properly support it
13 // UNSUPPORTED: gcc-5
14
15 // <variant>
16
17 // template <class ...Types> class variant;
18
19 #include <limits>
20 #include <type_traits>
21 #include <utility>
22 #include <variant>
23
24 #include "test_macros.h"
25
26 template <class Sequence>
27 struct make_variant_imp;
28
29 template <size_t ...Indices>
30 struct make_variant_imp<std::integer_sequence<size_t, Indices...>> {
31 template <size_t> using AlwaysChar = char;
32 using type = std::variant<AlwaysChar<Indices>...>;
33 };
34
35 template <size_t N>
36 using make_variant_t = typename make_variant_imp<std::make_index_sequence<N>>::type;
37
38 constexpr bool ExpectEqual =
39 #ifdef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
40 false;
41 #else
42 true;
43 #endif
44
45 template <class IndexType>
test_index_type()46 void test_index_type() {
47 using Lim = std::numeric_limits<IndexType>;
48 using T1 = make_variant_t<Lim::max() - 1>;
49 using T2 = make_variant_t<Lim::max()>;
50 static_assert((sizeof(T1) == sizeof(T2)) == ExpectEqual, "");
51 }
52
53 template <class IndexType>
test_index_internals()54 void test_index_internals() {
55 using Lim = std::numeric_limits<IndexType>;
56 static_assert(std::__choose_index_type(Lim::max() -1) !=
57 std::__choose_index_type(Lim::max()), "");
58 static_assert(std::is_same_v<
59 std::__variant_index_t<Lim::max()-1>,
60 std::__variant_index_t<Lim::max()>
61 > == ExpectEqual, "");
62 using IndexT = std::__variant_index_t<Lim::max()-1>;
63 using IndexLim = std::numeric_limits<IndexT>;
64 static_assert(std::__variant_npos<IndexT> == IndexLim::max(), "");
65 }
66
main(int,char **)67 int main(int, char**) {
68 test_index_type<unsigned char>();
69 // This won't compile due to template depth issues.
70 //test_index_type<unsigned short>();
71 test_index_internals<unsigned char>();
72 test_index_internals<unsigned short>();
73
74 return 0;
75 }
76