C
Chris Gordon-Smith
I am currently in India and have treated myself to the Indian reprint
of O'Reilly's "C++ In A Nutshell". (Books in India come in at 1/3 to
1/2 of the price in Britain.)
I thought that I would have a look at what Chapter 12 on grammar says
about Declarations and Definitions. Now I'm more baffled than when I
started.
Here's the problem.
Firstly some terminology. The book defines the symbol '::=' for a
complete definition of a particular item. It gives the following
example:-
function-specifier ::= explicit | inline | virtual
(The vertical bar represents a choice.)
The symbol ':=' indicates an incomplete definition, eg:=
function-specifier := inline.
Now, under the section on 'declaration', we have:-
translation-unit ::= [declaration-seq]
declaration-seq ::= declaration | declaration-seq declaration
(The [] brackets represent an optional item.)
What the above amounts to is the idea that a translation unit
(typically a source file and the code in all the #includes it brings
in) contains a series of zero or more declarations.
This implies that the following translation unit contains two
declarations and one definition:-
void MyFunc(int a); // In MyFunc.h: declaration without a definition
#include "MyFunc.h" // In MyFunc.cpp
void MyFunc(int a) { cout << a << endl; } // Declaration with a
definition.
So far so good (I think). Now the odd bit.
In the same (declaration) section of the book, declaration is defined
as follows:-
declaration ::= block-decl | function-decl | template-decl |
explicit-instantiation | explicit-specialization |
linkage-specification | namespace-defn
By contrast, under the section 'function' we have:-
declaration := function-defn
a) I can't find a definition for function-decl.
b) The two definitions for "declaration" seem to be contradictory. The
second says that a declaration can be a function-defn, the first says
that it cannot.
The idea that a declaration cannot include a function definition
contradicts the idea that a translation unit is (solely) a sequence of
declarations.
Can anyone throw any light on this. Is it an error in the book, or
have I misunderstood something?
Chris Gordon-Smith
Pune, India
of O'Reilly's "C++ In A Nutshell". (Books in India come in at 1/3 to
1/2 of the price in Britain.)
I thought that I would have a look at what Chapter 12 on grammar says
about Declarations and Definitions. Now I'm more baffled than when I
started.
Here's the problem.
Firstly some terminology. The book defines the symbol '::=' for a
complete definition of a particular item. It gives the following
example:-
function-specifier ::= explicit | inline | virtual
(The vertical bar represents a choice.)
The symbol ':=' indicates an incomplete definition, eg:=
function-specifier := inline.
Now, under the section on 'declaration', we have:-
translation-unit ::= [declaration-seq]
declaration-seq ::= declaration | declaration-seq declaration
(The [] brackets represent an optional item.)
What the above amounts to is the idea that a translation unit
(typically a source file and the code in all the #includes it brings
in) contains a series of zero or more declarations.
This implies that the following translation unit contains two
declarations and one definition:-
void MyFunc(int a); // In MyFunc.h: declaration without a definition
#include "MyFunc.h" // In MyFunc.cpp
void MyFunc(int a) { cout << a << endl; } // Declaration with a
definition.
So far so good (I think). Now the odd bit.
In the same (declaration) section of the book, declaration is defined
as follows:-
declaration ::= block-decl | function-decl | template-decl |
explicit-instantiation | explicit-specialization |
linkage-specification | namespace-defn
By contrast, under the section 'function' we have:-
declaration := function-defn
a) I can't find a definition for function-decl.
b) The two definitions for "declaration" seem to be contradictory. The
second says that a declaration can be a function-defn, the first says
that it cannot.
The idea that a declaration cannot include a function definition
contradicts the idea that a translation unit is (solely) a sequence of
declarations.
Can anyone throw any light on this. Is it an error in the book, or
have I misunderstood something?
Chris Gordon-Smith
Pune, India