This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[C++ PATCH] Allow void type as a literal type in C++14
- From: Marek Polacek <polacek at redhat dot com>
- To: Jason Merrill <jason at redhat dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Fri, 21 Nov 2014 18:55:51 +0100
- Subject: [C++ PATCH] Allow void type as a literal type in C++14
- Authentication-results: sourceware.org; auth=none
I noticed that C++14 [basic.types] says that "a type is a literal type
if it is: void, [...]". Yet our literal_type_p doesn't consider void
type as a literal type. The following is an attempt to fix that along
with a testcase. It seems that void was only added in C++14, so check
for cxx14 as well.
Bootstrapped/regtested on ppc64-linux, ok for trunk?
2014-11-21 Marek Polacek <polacek@redhat.com>
* constexpr.c (literal_type_p): Return true for void type in C++14.
* g++.dg/cpp0x/constexpr-function2.C: Limit dg-error to C++11.
* g++.dg/cpp0x/constexpr-neg1.C: Likewise.
* g++.dg/cpp1y/constexpr-void1.C: New test.
diff --git gcc/cp/constexpr.c gcc/cp/constexpr.c
index 2678223..0a258cf 100644
--- gcc/cp/constexpr.c
+++ gcc/cp/constexpr.c
@@ -59,7 +59,8 @@ literal_type_p (tree t)
{
if (SCALAR_TYPE_P (t)
|| TREE_CODE (t) == VECTOR_TYPE
- || TREE_CODE (t) == REFERENCE_TYPE)
+ || TREE_CODE (t) == REFERENCE_TYPE
+ || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
return true;
if (CLASS_TYPE_P (t))
{
diff --git gcc/testsuite/g++.dg/cpp0x/constexpr-function2.C gcc/testsuite/g++.dg/cpp0x/constexpr-function2.C
index 8c51c9d..95ee244 100644
--- gcc/testsuite/g++.dg/cpp0x/constexpr-function2.C
+++ gcc/testsuite/g++.dg/cpp0x/constexpr-function2.C
@@ -23,7 +23,7 @@ constexpr int area = squarei(side); // { dg-error "side|argument" }
int next(constexpr int x) // { dg-error "parameter" }
{ return x + 1; }
-constexpr void f(int x) // { dg-error "return type .void" }
+constexpr void f(int x) // { dg-error "return type .void" "" { target c++11_only } }
{ /* ... */ }
constexpr int prev(int x)
diff --git gcc/testsuite/g++.dg/cpp0x/constexpr-neg1.C gcc/testsuite/g++.dg/cpp0x/constexpr-neg1.C
index 35f5e8e..dfa1d6b 100644
--- gcc/testsuite/g++.dg/cpp0x/constexpr-neg1.C
+++ gcc/testsuite/g++.dg/cpp0x/constexpr-neg1.C
@@ -29,7 +29,7 @@ int next(constexpr int x) { // { dg-error "parameter" }
extern constexpr int memsz; // { dg-error "definition" }
// error: return type is void
-constexpr void f(int x) // { dg-error "void" }
+constexpr void f(int x) // { dg-error "void" "" { target c++11_only } }
{ /* ... */ }
// error: use of decrement
constexpr int prev(int x)
diff --git gcc/testsuite/g++.dg/cpp1y/constexpr-void1.C gcc/testsuite/g++.dg/cpp1y/constexpr-void1.C
index e69de29..10ef5bc 100644
--- gcc/testsuite/g++.dg/cpp1y/constexpr-void1.C
+++ gcc/testsuite/g++.dg/cpp1y/constexpr-void1.C
@@ -0,0 +1,13 @@
+// { dg-do compile { target c++14 } }
+
+struct S
+{
+ int i = 20;
+
+ constexpr void
+ foo (void)
+ {
+ if (i > 20)
+ __builtin_abort ();
+ }
+};
Marek