]> granicus.if.org Git - clang/commitdiff
More of N3652: don't add an implicit 'const' to 'constexpr' member functions when...
authorRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 25 Jun 2013 18:46:26 +0000 (18:46 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 25 Jun 2013 18:46:26 +0000 (18:46 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@184865 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaOverload.cpp
test/SemaCXX/constant-expression-cxx11.cpp
test/SemaCXX/constant-expression-cxx1y.cpp

index 29644b40b74db87503f805731af24dc667a9ba5a..d4d08b4bff8d86af81a7b4e78adc5608ce8be850 100644 (file)
@@ -1080,7 +1080,8 @@ static bool shouldTryToOverload(Sema &S, FunctionDecl *New, FunctionDecl *Old,
     // or non-static member function). Add it now, on the assumption that this
     // is a redeclaration of OldMethod.
     unsigned NewQuals = NewMethod->getTypeQualifiers();
-    if (NewMethod->isConstexpr() && !isa<CXXConstructorDecl>(NewMethod))
+    if (!S.getLangOpts().CPlusPlus1y && NewMethod->isConstexpr() &&
+        !isa<CXXConstructorDecl>(NewMethod))
       NewQuals |= Qualifiers::Const;
     if (OldMethod->getTypeQualifiers() != NewQuals)
       return true;
index 4e2b7edb7852a6348aa36a62f042562f0e75ae67..ca029d9d1246185e4815070c1ca0d22605951f3a 100644 (file)
@@ -1670,3 +1670,27 @@ namespace StmtExpr {
     });
   }
 }
+
+namespace VirtualFromBase {
+  struct S1 {
+    virtual int f() const;
+  };
+  struct S2 {
+    virtual int f();
+  };
+  template <typename T> struct X : T {
+    constexpr X() {}
+    double d = 0.0;
+    constexpr int f() { return sizeof(T); } // expected-warning {{will not be implicitly 'const' in C++1y}}
+  };
+
+  // Virtual f(), not OK.
+  constexpr X<X<S1>> xxs1;
+  constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
+  static_assert(p->f() == sizeof(X<S1>), ""); // expected-error {{constant expression}} expected-note {{virtual function call}}
+
+  // Non-virtual f(), OK.
+  constexpr X<X<S2>> xxs2;
+  constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
+  static_assert(q->f() == sizeof(S2), "");
+}
index 6043a1fffc341e4d15a72861d4418579169a5786..0d099c2120cf0a05c0224fc78547b60817024450 100644 (file)
@@ -790,3 +790,27 @@ namespace StmtExpr {
     return 0;
   }
 }
+
+namespace VirtualFromBase {
+  struct S1 {
+    virtual int f() const;
+  };
+  struct S2 {
+    virtual int f();
+  };
+  template <typename T> struct X : T {
+    constexpr X() {}
+    double d = 0.0;
+    constexpr int f() { return sizeof(T); }
+  };
+
+  // Non-virtual f(), OK.
+  constexpr X<X<S1>> xxs1;
+  constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
+  static_assert(p->f() == sizeof(S1), "");
+
+  // Virtual f(), not OK.
+  constexpr X<X<S2>> xxs2;
+  constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
+  static_assert(q->f() == sizeof(X<S2>), ""); // expected-error {{constant expression}} expected-note {{virtual function call}}
+}