Re: Does the null-pointer-constant occupy storage?
On Aug 8, 2:20 pm, Robert Hairgrove <rhairgr...@bigfoot.com> wrote:
{ removed double-spacing. -mod }
Further more required rule 16-2-1 states: "The preprocessor shall only
be used for file inclusion and include guards".
I.e. any #defines are forbidden, leaving no choice but to define NULL
as const int.
Now the question is where to put the definition of NULL?
The NULL macro (for the null pointer constant) is defined in the
<cstddef> library header file. A program should not define NULL on its
own.
I assume that this allows including STL headers? In which case NULL will
most likely already be defined in those headers, and there is no need to
worry about it.
Required rule 3-2-2 states: "The One Definition Rule shall
not be violated". And required rule 3-1-1 states: "It shall be
possible to include any header file in multiple translation units
without violating the one definition rule".
Doesn't rule 3-1-1 imply that the definition const int NULL = 0;
can not be placed in a header file?
Yes ... but you won't have to if you use standard headers.
No. There would be nothing wrong with defining a const int variable
(with some name other than NULL) in a header file. Indeed it makes a
lot of sense to define - in a common header file - any constant values
that are shared among source files.
If so, you would have to place it in every implementation
file where it is used, right? And that would violate rule 3-2-2?
Or am I missunderstanding the One Definition Rule?
No, that is correct.
No, that is incorrect. Const-qualified variables have internal linkage
by default. Therefore, a const int definition included by one source
file does -not- define the same variable when that same definition is
included by another source file. Each source file technically gets its
"own" const variable (albeit one with the same name and value as the
other source files). Therefore a const variable definition in a header
file does not violate the One Definition Rule.
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]