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