diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2014-11-08 05:07:16 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2014-11-08 05:07:16 +0000 |
commit | 0f0af19b058151690ec02d781d0039fb1fc38426 (patch) | |
tree | 628729499c9190870f32a30ddfea3d8c9db273fd /clang/test/SemaTemplate/cxx1z-fold-expressions.cpp | |
parent | 8f6dd44056ceb2821b7692f1a110f91866b1bf2b (diff) | |
download | bcm5719-llvm-0f0af19b058151690ec02d781d0039fb1fc38426.tar.gz bcm5719-llvm-0f0af19b058151690ec02d781d0039fb1fc38426.zip |
[c++1z] N4295: fold-expressions.
This is a new form of expression of the form:
(expr op ... op expr)
where one of the exprs is a parameter pack. It expands into
(expr1 op (expr2onwards op ... op expr))
(and likewise if the pack is on the right). The non-pack operand can be
omitted; in that case, an empty pack gives a fallback value or an error,
depending on the operator.
llvm-svn: 221573
Diffstat (limited to 'clang/test/SemaTemplate/cxx1z-fold-expressions.cpp')
-rw-r--r-- | clang/test/SemaTemplate/cxx1z-fold-expressions.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/clang/test/SemaTemplate/cxx1z-fold-expressions.cpp b/clang/test/SemaTemplate/cxx1z-fold-expressions.cpp new file mode 100644 index 00000000000..3934f011a35 --- /dev/null +++ b/clang/test/SemaTemplate/cxx1z-fold-expressions.cpp @@ -0,0 +1,78 @@ +// RUN: %clang_cc1 -std=c++1z -verify %s + +template<typename ...T> constexpr auto sum(T ...t) { return (... + t); } +template<typename ...T> constexpr auto product(T ...t) { return (t * ...); } +template<typename ...T> constexpr auto all(T ...t) { return (true && ... && t); } +template<typename ...T> constexpr auto dumb(T ...t) { return (false && ... && t); } + +static_assert(sum(1, 2, 3, 4, 5) == 15); +static_assert(product(1, 2, 3, 4, 5) == 120); +static_assert(!all(true, true, false, true, false)); +static_assert(all(true, true, true, true, true)); +static_assert(!dumb(true, true, true, true, true)); + +struct S { + int a, b, c, d, e; +}; +template<typename ...T> constexpr auto increment_all(T &...t) { + (++t, ...); +} +constexpr bool check() { + S s = { 1, 2, 3, 4, 5 }; + increment_all(s.a, s.b, s.c, s.d, s.e); + return s.a == 2 && s.b == 3 && s.c == 4 && s.d == 5 && s.e == 6; +} +static_assert(check()); + +template<int ...N> void empty() { + static_assert((N + ...) == 0); + static_assert((N * ...) == 1); + static_assert((N | ...) == 0); + static_assert((N & ...) == -1); + static_assert((N || ...) == false); + static_assert((N && ...) == true); + (N, ...); +} +template void empty<>(); + +// An empty fold-expression isn't a null pointer just because it's an integer +// with value 0. +template<int ...N> void null_ptr() { + void *p = (N + ...); // expected-error {{rvalue of type 'int'}} + void *q = (N | ...); // expected-error {{rvalue of type 'int'}} +} +template void null_ptr<>(); // expected-note {{in instantiation of}} + +template<int ...N> void bad_empty() { + (N - ...); // expected-error {{empty expansion for operator '-' with no fallback}} + (N / ...); // expected-error {{empty expansion for operator '/' with no fallback}} + (N % ...); // expected-error {{empty expansion for operator '%' with no fallback}} + (N = ...); // expected-error {{empty expansion for operator '=' with no fallback}} +} +template void bad_empty<>(); // expected-note {{in instantiation of}} + +template<int ...N> void empty_with_base() { + extern int k; + (k = ... = N); // expected-warning{{unused}} + + // FIXME: We misparse these. The first one looks a lot loke a declaration; + // it's not clear what's happening in the second one. + void (k = ... = N); // expected-error {{expected ')'}} expected-note {{to match}} + (void) (k = ... = N); // expected-error {{expected ')'}} expected-note {{to match}} +} +template void empty_with_base<>(); // expected-note {{in instantiation of}} +template void empty_with_base<1>(); + +struct A { + struct B { + struct C { + struct D { + int e; + } d; + } c; + } b; +} a; +template<typename T, typename ...Ts> constexpr decltype(auto) apply(T &t, Ts ...ts) { + return (t.*....*ts); +} +static_assert(&apply(a, &A::b, &A::B::c, &A::B::C::d, &A::B::C::D::e) == &a.b.c.d.e); |