aboutsummaryrefslogtreecommitdiff
path: root/test/SemaCXX
diff options
context:
space:
mode:
authorRichard Smith <richard-llvm@metafoo.co.uk>2013-05-06 06:51:17 +0000
committerRichard Smith <richard-llvm@metafoo.co.uk>2013-05-06 06:51:17 +0000
commit692eafd2052fb6ca581530d6f3569eea9520a508 (patch)
treeeb574b7aab5fe2222357ba8188f852a945d497ce /test/SemaCXX
parentc194c95036b7bf1281a6f2ed683f7c85ee5d2c20 (diff)
C++1y: support range-based for loops in constant expressions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181184 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/SemaCXX')
-rw-r--r--test/SemaCXX/constant-expression-cxx1y.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/test/SemaCXX/constant-expression-cxx1y.cpp b/test/SemaCXX/constant-expression-cxx1y.cpp
index d8cfb1c297..60ec82062d 100644
--- a/test/SemaCXX/constant-expression-cxx1y.cpp
+++ b/test/SemaCXX/constant-expression-cxx1y.cpp
@@ -402,4 +402,58 @@ namespace loops {
return true;
}
static_assert(cond(), "");
+
+ constexpr int range_for() {
+ int arr[] = { 1, 2, 3, 4, 5 };
+ int sum = 0;
+ for (int x : arr)
+ sum = sum + x;
+ return sum;
+ }
+ static_assert(range_for() == 15, "");
+
+ template<int...N> struct ints {};
+ template<typename A, typename B> struct join_ints;
+ template<int...As, int...Bs> struct join_ints<ints<As...>, ints<Bs...>> {
+ using type = ints<As..., sizeof...(As) + Bs...>;
+ };
+ template<unsigned N> struct make_ints {
+ using type = typename join_ints<typename make_ints<N/2>::type, typename make_ints<(N+1)/2>::type>::type;
+ };
+ template<> struct make_ints<0> { using type = ints<>; };
+ template<> struct make_ints<1> { using type = ints<0>; };
+
+ struct ignore { template<typename ...Ts> constexpr ignore(Ts &&...) {} };
+
+ template<typename T, unsigned N> struct array {
+ constexpr array() : arr{} {}
+ template<typename ...X>
+ constexpr array(X ...x) : arr{} {
+ init(typename make_ints<sizeof...(X)>::type{}, x...);
+ }
+ template<int ...I, typename ...X> constexpr void init(ints<I...>, X ...x) {
+ ignore{arr[I] = x ...};
+ }
+ T arr[N];
+ struct iterator {
+ T *p;
+ constexpr explicit iterator(T *p) : p(p) {}
+ constexpr bool operator!=(iterator o) { return p != o.p; }
+ constexpr iterator &operator++() { ++p; return *this; }
+ constexpr T &operator*() { return *p; }
+ };
+ constexpr iterator begin() { return iterator(arr); }
+ constexpr iterator end() { return iterator(arr + N); }
+ };
+
+ constexpr int range_for_2() {
+ array<int, 5> arr { 1, 2, 3, 4, 5 };
+ int sum = 0;
+ for (int k : arr) {
+ sum = sum + k;
+ if (sum > 8) break;
+ }
+ return sum;
+ }
+ static_assert(range_for_2() == 10, "");
}