xref: /llvm-project/libcxx/test/std/utilities/variant/variant.helpers/variant_alternative.pass.cpp (revision d4b59a05fc7507cf69993109443dc5af47ae4fa8)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // <variant>
12 
13 // template <size_t I, class T> struct variant_alternative; // undefined
14 // template <size_t I, class T> struct variant_alternative<I, const T>;
15 // template <size_t I, class T> struct variant_alternative<I, volatile T>;
16 // template <size_t I, class T> struct variant_alternative<I, const volatile T>;
17 // template <size_t I, class T>
18 //   using variant_alternative_t = typename variant_alternative<I, T>::type;
19 //
20 // template <size_t I, class... Types>
21 //    struct variant_alternative<I, variant<Types...>>;
22 
23 #include <memory>
24 #include <type_traits>
25 #include <variant>
26 
27 #include "test_macros.h"
28 #include "variant_test_helpers.h"
29 
30 template <class V, size_t I, class E> void test() {
31   static_assert(
32       std::is_same_v<typename std::variant_alternative<I, V>::type, E>, "");
33   static_assert(
34       std::is_same_v<typename std::variant_alternative<I, const V>::type,
35                      const E>,
36       "");
37   static_assert(
38       std::is_same_v<typename std::variant_alternative<I, volatile V>::type,
39                      volatile E>,
40       "");
41   static_assert(
42       std::is_same_v<
43           typename std::variant_alternative<I, const volatile V>::type,
44           const volatile E>,
45       "");
46   static_assert(std::is_same_v<std::variant_alternative_t<I, V>, E>, "");
47   static_assert(std::is_same_v<std::variant_alternative_t<I, const V>, const E>,
48                 "");
49   static_assert(
50       std::is_same_v<std::variant_alternative_t<I, volatile V>, volatile E>,
51       "");
52   static_assert(std::is_same_v<std::variant_alternative_t<I, const volatile V>,
53                                const volatile E>,
54                 "");
55 }
56 
57 int main(int, char**) {
58   {
59     using V = std::variant<int, void *, const void *, long double>;
60     test<V, 0, int>();
61     test<V, 1, void *>();
62     test<V, 2, const void *>();
63     test<V, 3, long double>();
64   }
65 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
66   {
67     using V = std::variant<int, int &, const int &, int &&, long double>;
68     test<V, 0, int>();
69     test<V, 1, int &>();
70     test<V, 2, const int &>();
71     test<V, 3, int &&>();
72     test<V, 4, long double>();
73   }
74 #endif
75 
76   return 0;
77 }
78