10

While reading the standard, I was really surprised to see that actually, the name is optional in a declaration:

struct Magic {};
int main() {
  int; // well-formed
  Magic; // well-formed
}

For those interested to read the standard, it's because in a simple-declaration, the init-declarator-list is optional. Here's a demo.

And so I really want to know why this is allowed. Why would I ever want to do that? It really makes no sense to me. My reasoning is that, because if it makes no sense, why allow it?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
  • @ChrisSprague so, "because that's the same grammar for function parameters and it's useful there"? – Quentin Jan 10 '18 at 17:11
  • 2
    @ChrisSprague That would make sense. But the *parameter-declaration-clause* already uses a separate grammar altogether (*abstract-declarator*) together with *declator*. There is no need for that to be the case for *declarator*, I gather. – Rakete1111 Jan 10 '18 at 17:15
  • A user defined object could do some work in constructor/destructor (eg create a database transaction). –  Jan 10 '18 at 17:17
  • @manni66 That's not the case here, no objects are created. – HolyBlackCat Jan 10 '18 at 17:17
  • Related: `struct {something};` counts as a declaration declaring nothing too. – HolyBlackCat Jan 10 '18 at 17:18
  • I wonder why the standard still allows this? Maybe so old broken code is thill allowed to compile? – Jabberwocky Jan 10 '18 at 17:20
  • 4
    `-pedantic-errors` is your friend. – T.C. Jan 10 '18 at 17:23
  • 2
    That demo doesn’t show it’s well-formed. For one thing, I think Clang only claims to be conforming with `-pedantic`. For another, both GCC and Clang say warnings count as diagnostic messages, and all the standard requires for ill-formed code is a diagnostic (usually); the compiler can still emit a program if it wants. – Daniel H Jan 10 '18 at 17:25
  • I believe they are called or referred to as anonymous objects. Also see [What is an Anonymous Object?](https://stackoverflow.com/q/5330287/608639), [Is it possible to pass an “unnamed” variable to a function?](https://stackoverflow.com/q/3394797/608639), [Passing an anonymous variable by reference](https://stackoverflow.com/q/19875697/608639), [C++ anonymous variables](https://stackoverflow.com/q/461062/608639), etc. – jww Jan 10 '18 at 17:32

1 Answers1

14

According to the C++ Standard (7 Declarations)

5 In a simple-declaration, the optional init-declarator-list can be omitted only when declaring a class (Clause 9) or enumeration (7.2), that is, when the decl-specifier-seq contains either a class-specifier, an elaborated-type-specifier with a class-key (9.1), or an enum-specifier....

Thus this code

int main() {
  int; // well-formed
  Magic; // well-formed
}

is ill-formed.

Moreover formally these ill-formed declarations are definitions according to the paragraph #2 of the section 3.1 Declarations and definitions and obviously as such do not make sense.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    Nice one. It's funny how most people just believed the OP and didn't try to compile the code with proper flags. – HolyBlackCat Jan 10 '18 at 17:27
  • I was looking in declarators for something like this, did not expect this to be in declarations :) Thanks – Rakete1111 Jan 10 '18 at 17:45
  • This explanation only goes so far because (what's supposed to be) the *init-declarator-list* might not be completely omitted. Rakete1111's comment to the question applies in this case, instead, a *declarator* is required (and not an *abstract-declarator*), which must ultimately contain an *id-expression*. Example, `*` is a valid *abstract-declarator*, but not a valid *declarator*, so `int *;` is not a valid *simple-declaration*. – Arne Vogel Jan 10 '18 at 18:29