Debian Codegraph @

clang/test/SemaCXX/discrim-union.cpp

raw
1 // RUN: %clang_cc1 -std=c++11 %s -fsyntax-only -fcxx-exceptions -verify
2
3 // expected-no-diagnostics
4
5 template<typename T> struct remove_reference { typedef T type; };
6 template<typename T> struct remove_reference<T&> { typedef T type; };
7 template<typename T> struct remove_reference<T&&> { typedef T type; };
8
9 template<typename T> constexpr T &&forward(typename remove_reference<T>::type &t) noexcept { return static_cast<T&&>(t); }
10 template<typename T> constexpr T &&forward(typename remove_reference<T>::type &&t) noexcept { return static_cast<T&&>(t); }
11 template<typename T> constexpr typename remove_reference<T>::type &&move(T &&t) noexcept { return static_cast<typename remove_reference<T>::type&&>(t); }
12
13 template<typename T> T declval() noexcept;
14
15 namespace detail {
16 template<unsigned N> struct select {}; // : integral_constant<unsigned, N> {};
17 template<typename T> struct type {};
18
19 template<typename...T> union either_impl;
20
21 template<> union either_impl<> {
22 void get(...);
23 void destroy(...) { throw "logic_error"; }
24 };
25
26 template<typename T, typename...Ts> union either_impl<T, Ts...> {
27 private:
28 T val;
29 either_impl<Ts...> rest;
30 typedef either_impl<Ts...> rest_t;
31
32 public:
33 constexpr either_impl(select<0>, T &&t) : val(move(t)) {}
34
35 template<unsigned N, typename U>
36 constexpr either_impl(select<N>, U &&u) : rest(select<N-1>(), move(u)) {}
37
38 constexpr static unsigned index(type<T>) { return 0; }
39 template<typename U>
40 constexpr static unsigned index(type<U> t) {
41 return decltype(rest)::index(t) + 1;
42 }
43
44 void destroy(unsigned elem) {
45 if (elem)
46 rest.destroy(elem - 1);
47 else
48 val.~T();
49 }
50
51 constexpr const T &get(select<0>) const { return val; }
52 template<unsigned N> constexpr const decltype(static_cast<const rest_t&>(rest).get(select<N-1>{})) get(select<N>) const {
53 return rest.get(select<N-1>{});
54 }
55 };
56 }
57
58 template<typename T>
59 struct a {
60 T value;
61 template<typename...U>
62 constexpr a(U &&...u) : value{forward<U>(u)...} {}
63 };
64 template<typename T> using an = a<T>;
65
66 template<typename T, typename U> T throw_(const U &u) { throw u; }
67
68 template<typename...T>
69 class either {
70 unsigned elem;
71 detail::either_impl<T...> impl;
72 typedef decltype(impl) impl_t;
73
74 public:
75 template<typename U>
76 constexpr either(a<U> &&t) :
77 elem(impl_t::index(detail::type<U>())),
78 impl(detail::select<impl_t::index(detail::type<U>())>(), move(t.value)) {}
79
80 // Destruction disabled to allow use in a constant expression.
81 // FIXME: declare a destructor iff any element has a nontrivial destructor
82 //~either() { impl.destroy(elem); }
83
84 constexpr unsigned index() const noexcept { return elem; }
85
86 template<unsigned N> using const_get_result =
87 decltype(static_cast<const impl_t&>(impl).get(detail::select<N>{}));
88
89 template<unsigned N>
90 constexpr const_get_result<N> get() const {
91 // Can't just use throw here, since that makes the conditional a prvalue,
92 // which means we return a reference to a temporary.
93 return (elem != N ? throw_<const_get_result<N>>("bad_either_get")
94 : impl.get(detail::select<N>{}));
95 }
96
97 template<typename U>
98 constexpr const U &get() const {
99 return get<impl_t::index(detail::type<U>())>();
100 }
101 };
102
103 typedef either<int, char, double> icd;
104 constexpr icd icd1 = an<int>(4);
105 constexpr icd icd2 = a<char>('x');
106 constexpr icd icd3 = a<double>(6.5);
107
108 static_assert(icd1.get<int>() == 4, "");
109 static_assert(icd2.get<char>() == 'x', "");
110 static_assert(icd3.get<double>() == 6.5, "");
111
112 struct non_triv {
113 constexpr non_triv() : n(5) {}
114 int n;
115 };
116 constexpr either<const icd*, non_triv> icd4 = a<const icd*>(&icd2);
117 constexpr either<const icd*, non_triv> icd5 = a<non_triv>();
118
119 static_assert(icd4.get<const icd*>()->get<char>() == 'x', "");
120 static_assert(icd5.get<non_triv>().n == 5, "");