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.]
(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.
Mine.
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
Fixed.