Topic: external and internal linkage
Author: richards_corden@hotmail.com
Date: Wed, 27 Jun 2001 17:42:42 GMT Raw View
In the following:
foo.cpp
--------
void foo ()
{
extern int x;
}
static int x;
>From my reading of 3.5 paragraph 6 and 7
the block scope declaration adds an entity to
the enclosing namespace with external linkage.
3.5 paragraph 9 says that names refer to the
same object only if they both have external
linkage or they both have internal linkage...
Does this mean that the two declarations
of x refer to different objects?
Thanks,
Richard
--
Richard Corden
To reply remove 's' from address
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: iltchenko@yahoo.com (Andrei Iltchenko)
Date: Fri, 29 Jun 2001 19:44:24 GMT Raw View
richards_corden@hotmail.com wrote in message news:<8366dixh5i.fsf@leinster.programmingresearch.ie>...
> In the following:
>
> foo.cpp
> --------
> void foo ()
> {
> extern int x;
> }
> static int x;
>
> From my reading of 3.5 paragraph 6 and 7
> the block scope declaration adds an entity to
> the enclosing namespace with external linkage.
No. The block scope declaration 'extern int x;' introduces the name
'x' into the block that encloses it (it doesn't introduce the name
into the innermost enclosing namespace, nor does it add any entity to
that namespace). The name denotes an entity (in this case the entity
is a variable) which is a member of the innermost enclosing namespace.
Given that at the point of its declaration in the block there are no
visible declarations of 'x' that are declared prior to the
function-definition for 'foo', the name 'x' receives external linkage.
The function-definition is then followed by a definition for 'x',
which introduces the name 'x' into its immediately enclosing namespace
scope and assigns the name internal linkage. This results in two names
being the same, denoting the same variable, and having different
linkages, which is ill-formed since the linkages supplied by
successive declarations for a given entity shall agree. See 7.1.1/7.
> 3.5 paragraph 9 says that names refer to the
> same object only if they both have external
> linkage or they both have internal linkage...
Correct.
> Does this mean that the two declarations
> of x refer to different objects?
As explained above they do refer to the same object, but they violate
the fundamental language rule thus rendering the piece of code
ill-formed.
Regards,
Andrei Iltchenko.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]