| 1 |
// PR c++/58176 |
| 2 |
// { dg-do compile { target c++11 } } |
| 3 |
|
| 4 |
// Nil |
| 5 |
struct nil_ { constexpr nil_ () {} }; |
| 6 |
constexpr nil_ nil; |
| 7 |
|
| 8 |
// Cons |
| 9 |
template <class H, class T = nil_> |
| 10 |
struct cons_ { |
| 11 |
using head_ = H; |
| 12 |
using tail_ = T; |
| 13 |
|
| 14 |
H head; |
| 15 |
T tail; |
| 16 |
|
| 17 |
constexpr cons_() {} |
| 18 |
constexpr cons_(H const &h, T const &t) : head(h), tail(t) {} |
| 19 |
}; |
| 20 |
template <class H, class T = nil_> |
| 21 |
constexpr cons_<H, T> cons (H const &h, T const &t = nil) { return |
| 22 |
cons_<H,T>(h,t); } |
| 23 |
|
| 24 |
// List |
| 25 |
template <class... T> struct list_s; |
| 26 |
template <class H, class... T> |
| 27 |
struct list_s<H, T...> { |
| 28 |
using type = cons_<H, typename list_s<T...>::type>; |
| 29 |
}; |
| 30 |
template <> |
| 31 |
struct list_s<> { |
| 32 |
using type = nil_; |
| 33 |
}; |
| 34 |
template <class... T> |
| 35 |
using list_ = typename list_s<T...>::type; |
| 36 |
constexpr nil_ list () { return nil; } |
| 37 |
template <class H, class... T> |
| 38 |
constexpr list_<H, T...> list (H h, T... t) { return cons(h, list(t...)); } |
| 39 |
|
| 40 |
constexpr auto l1 = list("monkey", 123.4, cons(1, 2), nullptr); |