]> granicus.if.org Git - clang/commitdiff
Only objects are declared const by a constexpr specifier, per C++0x [dcl.constexpr]p9
authorPeter Collingbourne <peter@pcc.me.uk>
Sun, 20 Mar 2011 08:06:45 +0000 (08:06 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Sun, 20 Mar 2011 08:06:45 +0000 (08:06 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@127967 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaType.cpp
test/SemaCXX/cxx0x-constexpr-const.cpp [new file with mode: 0644]

index dc2d3ac518bf192e4f0025f51368c9f136488075..88541c9508ecb6f4fa0cb84c0bb629f4dd370906 100644 (file)
@@ -2116,8 +2116,10 @@ TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
   // Diagnose any ignored type attributes.
   if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
 
-  // If there's a constexpr specifier, treat it as a top-level const.
-  if (D.getDeclSpec().isConstexprSpecified()) {
+  // C++0x [dcl.constexpr]p9:
+  //  A constexpr specifier used in an object declaration declares the object
+  //  as const. 
+  if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
     T.addConst();
   }
 
diff --git a/test/SemaCXX/cxx0x-constexpr-const.cpp b/test/SemaCXX/cxx0x-constexpr-const.cpp
new file mode 100644 (file)
index 0000000..79e6dda
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s
+
+constexpr int x = 1;
+constexpr int id(int x) { return x; }
+
+void foo(void) {
+  x = 2; // expected-error {{read-only variable is not assignable}}
+  int (*idp)(int) = id;
+}
+