6
struct t_empty {
};

This appears to compile properly in C++ but not C. (at least with the TI 28xx DSP compiler, where it emits the error "expected a declaration") Is this mentioned somewhere in the C standards, or is my compiler broken?

Jason S
  • 184,598
  • 164
  • 608
  • 970
  • It compiles fine using gcc 4.2... – Vladimir May 17 '10 at 14:38
  • 1
    See for See http://stackoverflow.com/questions/1626446/what-is-the-size-of-an-empty-struct-in-c for the discussion concerning C – Artefacto May 17 '10 at 14:44
  • 1
    one of comments from that link: "Empty structs are a GCC extension.". Thanks :) – Vladimir May 17 '10 at 14:48
  • @Artefacto: why did you delete your post? :-( it was useful. – Jason S May 17 '10 at 14:49
  • 1
    @Jason S: It was wrong. The accepted answer in http://stackoverflow.com/questions/1626446/what-is-the-size-of-an-empty-struct-in-c gets it right. – Artefacto May 17 '10 at 14:56
  • A question for SO readers: given that gcc (wrongly) accepts this syntax, has anyone actually found a use for this extension in their code? – Joseph Quinsey May 17 '10 at 16:44
  • The situation that produced my question was a piece of code emitted by a code-generation tool. It defines a structure based on some metadata described elsewhere. The metadata in question was empty. – Jason S May 17 '10 at 16:50
  • @AndreyT honestly when i wrote my answer to the other question i wasn't *really* sure about it (i worded my answer carefully to avoid saying wrong things), since in C you can cause undefined behavior but still guaranteed to receive diagnostics (unlike in C++). But now i'm actually sure that behavior is not undefined for `struct A { };` - it's just a syntax violation, because there is no empty `struct-declaration-list`. After diagnosing a non-conforming program the implementation can do anything it likes anyway, so it doesn't really matter whether the semantic constraint still applies or not :) – Johannes Schaub - litb May 17 '10 at 17:58
  • However it *does* matter that the syntax violation applies, since it determines whether a diagnostic is required or not :) So i too think that missing to say it's a syntax violation in an answer makes an answer wrong, i think. – Johannes Schaub - litb May 17 '10 at 17:59

1 Answers1

15

Empty struct is a syntax error in C. The grammar of C language is written so that it prohibits empty structs. I.e. you won't find it stated in the standard explicitly, it just follows from the grammar.

In C++ empty classes are indeed legal.

P.S. Note, that often you might see the quote from the C standard that says "If the struct-declaration-list contains no named members, the behavior is undefined.", which is presented as the portion of the document that prohibits empty structs. In reality, empty structs are, again, prohibited by the grammar. So a literally empty struct (as in your question) is a syntax error, not undefined behavior. The above quote from the standard applies to a different situation: a struct with no named members. A struct can end up non-empty, but at the same time with no named members if all members are unnamed bitfields

struct S {
  int : 5;
};

In the above case the behavior is undefined. This is what the above quote is talking about.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765