Bug 55250 - [C++0x] enum declarations within constexpr function are allowed, constexpr declarations are not
Summary: [C++0x] enum declarations within constexpr function are allowed, constexpr de...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.1
: P3 normal
Target Milestone: 5.0
Assignee: Paolo Carlini
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2012-11-09 12:27 UTC by Joel Yliluoma
Modified: 2014-10-06 16:16 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-11-09 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joel Yliluoma 2012-11-09 12:27:55 UTC
The following code compiles in GCC without warnings on -Wall -W -pedantic:
  constexpr int Test1(int x)   { enum { y = 1 };       return x+y; }

The following one does not:
  constexpr int Test2(int x)   { constexpr int y = 1;  return x+y; }

For the second code, GCC gives "error: body of constexpr function 'constexpr int Test2(int)' not a return-statement"

In comparison, Clang++ gives an error for Test1: "error: types cannot be defined in a constexpr function", and for Test2: "error: variables cannot be declared in a constexpr function" for Test2.

Now, reading http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2235.pdf , it is not entirely unambiguous which behavior is correct.

While I would like that both samples worked without warnings, I suggest that attempting to declare an enum within a constexpr function will be made a -pedantic warning.

[Tested on GCC 4.6.3 through 4.7.2. On GCC 4.5.3, both functions compiled without warnings.]
Comment 1 Jonathan Wakely 2012-11-09 12:51:52 UTC
(In reply to comment #0)
> Now, reading http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2235.pdf
> , it is not entirely unambiguous which behavior is correct.

That paper is five years old, so not relevant to what C++11 allows.

The standard says:

The definition of a constexpr function shall satisfy the following constraints:
[...]
— its function-body shall be = delete, = default, or a compound-statement that contains only
— null statements,
— static_assert-declarations
— typedef declarations and alias-declarations that do not define classes or enumerations,
[...]

so it's ill-formed to declare an enumeration.
Comment 2 Paolo Carlini 2014-10-03 18:01:45 UTC
Mine.
Comment 3 paolo@gcc.gnu.org 2014-10-06 16:14:13 UTC
Author: paolo
Date: Mon Oct  6 16:13:41 2014
New Revision: 215954

URL: https://gcc.gnu.org/viewcvs?rev=215954&root=gcc&view=rev
Log:
/cp
2014-10-06  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/55250
	* semantics.c (check_constexpr_bind_expr_vars): New.
	(check_constexpr_ctor_body, massage_constexpr_body): Use it.
	(build_constexpr_constructor_member_initializers): Handle
	BIND_EXPR in the main conditional.

/testsuite
2014-10-06  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/55250
	* g++.dg/cpp0x/constexpr-type-decl1.C: New.
	* g++.dg/cpp0x/constexpr-type-def1.C: Likewise.
	* g++.dg/cpp1y/constexpr-type-def1.C: Likewise.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-type-decl1.C
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-type-def1.C
    trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-type-def1.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/semantics.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Paolo Carlini 2014-10-06 16:16:04 UTC
Fixed.