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