Boost logo

Boost Users :

From: Brian Allison (brian_at_[hidden])
Date: 2005-07-01 06:13:46




Dave Steffen wrote:
Gen Zhang writes:
 > I believe The Book specifically allows an optional semicolon at the end
 > of a function declaration in the global scope. They are illegal in a
 > class. And necessary at the end of a class/struct definition. I have a
 > friend who runs into this problem a lot. :D

 Yeah, this is one question - is it legal?

According to the grammar listed as Appendix A of "The C++ Programming Language, Special Edition", by Stroustrup...

  p. 798

        A translation-unit, often called a source file, is a sequence of declarations:

            translation-unit:
                declaration-seqopt


 So 0 or more declaration-seq makes up a source file.

  p. 803 (bottom of page) lists the production rule for a declaration-seq:

            declaration-seq:
                declaration
                declaration-seq declaration


  p. 804 (top of page) tells us the production rule for a declaration rule. a declaration may be entirely replaced by a block-declaration.

  The next production rule tells us that a block-declaration may be entirely replaced by a simple-declaration.

  The next production rule is:

            simple-declaration:
                decl-specifier-seqopt init-declarator-listopt  ;


  Note that the only thing that isn't optional is the semicolon.

  Presuming the book has no typos, presuming the grammar is a valid representation of the C++ specification, then it would seem that a spurious semicolon at the end of any global declaration is itself a global declaration and is not an error. I find it distasteful, but having the extra semicolon does seem to be valid - if my presumptions are accurate.


  And while GCC's acceptance doesn't mean it's valid C++... attached is the obExample.

Brian

// Compiled (gcc 3.4.3) and run on a Red Hat system (2.4.20-8 kernel) with:
// g++ -o feh feh.C && ./feh
//
/* Next line is sole output.
feh!
*/

#include <iostream>

int main(int argc, char **argv)
{
  std::cerr << "feh!\n";
  return 0;
}

;

;


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net