]> granicus.if.org Git - clang/commitdiff
constexpr: Implement DR1358: An instantiation of a constexpr function which
authorRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 5 Feb 2012 02:30:54 +0000 (02:30 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 5 Feb 2012 02:30:54 +0000 (02:30 +0000)
can't produce a constant expression is not ill-formed (so long as some
instantiation of that function can produce a constant expression).

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

include/clang/Sema/Sema.h
lib/Sema/SemaDecl.cpp
lib/Sema/SemaDeclCXX.cpp
test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p5.cpp

index d2d61abc7a7c9c8fb1edcff7023c4f9d4f68dfbb..8aca77f51ab751341f0c2431532c16c1aac4b563 100644 (file)
@@ -1016,7 +1016,8 @@ public:
   };
   bool CheckConstexprFunctionDecl(const FunctionDecl *FD,
                                   CheckConstexprKind CCK);
-  bool CheckConstexprFunctionBody(const FunctionDecl *FD, Stmt *Body);
+  bool CheckConstexprFunctionBody(const FunctionDecl *FD, Stmt *Body,
+                                  bool IsInstantiation);
 
   void DiagnoseHiddenVirtualMethods(CXXRecordDecl *DC, CXXMethodDecl *MD);
   // Returns true if the function declaration is a redeclaration
index 6b92b0ad67c6b67c464f8552a281a0daa81ea0a6..172e931153d6381a63caca10df2a2f6d814dd18b 100644 (file)
@@ -7302,7 +7302,7 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
     }
 
     if (FD && FD->isConstexpr() && !FD->isInvalidDecl() &&
-        !CheckConstexprFunctionBody(FD, Body))
+        !CheckConstexprFunctionBody(FD, Body, IsInstantiation))
       FD->setInvalidDecl();
 
     assert(ExprCleanupObjects.empty() && "Leftover temporaries in function");
index a52d44c4f2e403cdfd395fe58654bba52bbad0a6..10883af4b4111266dc76e37a1a0a90e0727c6070 100644 (file)
@@ -837,7 +837,8 @@ static void CheckConstexprCtorInitializer(Sema &SemaRef,
 /// the permitted types of statement. C++11 [dcl.constexpr]p3,p4.
 ///
 /// \return true if the body is OK, false if we have diagnosed a problem.
-bool Sema::CheckConstexprFunctionBody(const FunctionDecl *Dcl, Stmt *Body) {
+bool Sema::CheckConstexprFunctionBody(const FunctionDecl *Dcl, Stmt *Body,
+                                      bool IsInstantiation) {
   if (isa<CXXTryStmt>(Body)) {
     // C++11 [dcl.constexpr]p3:
     //  The definition of a constexpr function shall satisfy the following
@@ -989,7 +990,7 @@ bool Sema::CheckConstexprFunctionBody(const FunctionDecl *Dcl, Stmt *Body) {
   // can't produce constant expressions.
   llvm::SmallVector<PartialDiagnosticAt, 8> Diags;
   if (!Context.getSourceManager().isInSystemHeader(Dcl->getLocation()) &&
-      !Expr::isPotentialConstantExpr(Dcl, Diags)) {
+      !IsInstantiation && !Expr::isPotentialConstantExpr(Dcl, Diags)) {
     Diag(Dcl->getLocation(), diag::err_constexpr_function_never_constant_expr)
       << isa<CXXConstructorDecl>(Dcl);
     for (size_t I = 0, N = Diags.size(); I != N; ++I)
index 43fc887b0053d38ce98bfcb130fd877b73525fff..4e7608ec62a52d17549874a00fa8a115015405d2 100644 (file)
@@ -78,4 +78,29 @@ constexpr bool BcpCall(int n) {
 }
 static_assert(BcpCall(0), "");
 
+// DR1311: A function template which can produce a constant expression, but
+// for which a particular specialization cannot, is ok.
+template<typename T> constexpr T cmin(T a, T b) {
+  return a < b ? a : b;
+}
+int n = cmin(3, 5); // ok
+
+struct X {
+  constexpr X() {}
+  bool operator<(X); // not constexpr
+};
+
+X x = cmin(X(), X()); // ok, not constexpr
+
+// Same with other temploids.
+template<typename T>
+struct Y {
+  constexpr Y() {}
+  constexpr int get() { return T(); }
+};
+struct Z { operator int(); };
+
+int y1 = Y<int>().get(); // ok
+int y2 = Y<Z>().get(); // ok
+
 }