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