Reputation: 277
By mistake, I wrote something along the lines of constexpr bool{};
, and while GCC and Clang rejected this, MSVC was more than happy to compile it (see Godbolt). From my understanding, functions (and thus constructors) evaluated at compile time cannot have side effects, therefore this can never have any effect, but is it indeed ill-formed?
(In my experience, MSVC tends to be wrong, but in this specific case I didn’t find where the standard forbids this.)
Upvotes: 8
Views: 374
Reputation: 119457
That's just not valid syntax. It is "forbidden" by the standard by virtue of not being a possible grammar production.
A declaration such as
constexpr bool b{};
is a simple-declaration and has the syntax decl-specifier-seq init-declarator-list(opt) ;
(see C++17 [dcl.dcl]/1). The keyword constexpr
is a decl-specifier, and so is bool
(although only some decl-specifiers have an effect on the type; bool
does, but constexpr
does not).
The rest of the declaration, b{}
, is an init-declarator, which consists of a declarator plus an optional initializer, which in this case is {}
. (See [dcl.decl]/1.) The declarator is b
. In general, a declarator must contain an identifier such as b
. See [dcl.decl]/4.
There is a similar grammar production called an abstract-declarator which lacks an identifier (See [dcl.name]/1). Abstract declarators are allowed in particular contexts, such as when writing down a type-id, or in a parameter-declaration-clause (function parameters are allowed to be unnamed). However, an init-declarator must contain a declarator, not an abstract-declarator.
There is no other grammar production that would match constexpr bool{};
either.
Upvotes: 9