Semicolon after class declaration braces

The link provided by @MichaelHaren appears to provide the root cause. The semicolon (as others have pointed out) is inherited from C. But that doesn’t explain why C used it in the first place. The discussion includes this gem of an example:

struct fred { int x; long y; }; 
main() 
{ 
  return 0; 
} 

Older versions of C had an implicit int return type from a function unless declared otherwise. If we omit the ; at the end of the structure definition, we’re not only defining a new type fred, but also declaring that main() will return an instance of fred. I.e. the code would be parsed like this:

struct fred { int x; long y; } main()
{ 
  return 0; /* invalid return type, expected fred type */
} 

Leave a Comment