]> granicus.if.org Git - clang/commitdiff
Don't reject dependent range-based for loops in constexpr functions. The loop
authorRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 15 Nov 2013 02:29:26 +0000 (02:29 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 15 Nov 2013 02:29:26 +0000 (02:29 +0000)
variable isn't really uninitialized, it's just not initialized yet.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194767 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDeclCXX.cpp
test/SemaCXX/constant-expression-cxx1y.cpp

index 660641eec6cadb5e205cda1498586b09049a0c1c..1eec2318e2b13dd15f23f7f79e4198764e253d5a 100644 (file)
@@ -880,7 +880,7 @@ static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl,
               diag::err_constexpr_local_var_non_literal_type,
               isa<CXXConstructorDecl>(Dcl)))
           return false;
-        if (!VD->hasInit()) {
+        if (!VD->hasInit() && !VD->isCXXForRangeDecl()) {
           SemaRef.Diag(VD->getLocation(),
                        diag::err_constexpr_local_var_no_init)
             << isa<CXXConstructorDecl>(Dcl);
index 136f7b198a8646f37db4661734224442039d8110..521526ddba4487bb11fe238dad88ba81bf158011 100644 (file)
@@ -898,3 +898,16 @@ namespace PR17615 {
   };
   constexpr int k = A().r; // expected-error {{constant expression}} expected-note {{in call to}}
 }
+
+namespace PR17331 {
+  template<typename T, unsigned int N>
+  constexpr T sum(const T (&arr)[N]) {
+    T result = 0;
+    for (T i : arr)
+      result += i;
+    return result;
+  }
+
+  constexpr int ARR[] = { 1, 2, 3, 4, 5 };
+  static_assert(sum(ARR) == 15, "");
+}