Linkage question

This is a discussion on Linkage question within the C++ Programming forums, part of the General Programming Boards category; I'm a bit puzzled by the following from chapter 3.5 of the C++ standard:
"The name of a function declared ...

Linkage question

I'm a bit puzzled by the following from chapter 3.5 of the C++ standard:

"The name of a function declared in block scope, and the name of an object declared by a block scope extern declaration,
have linkage. If there is a visible declaration of an entity with linkage having the same name and type, ignoring entities
declared outside the innermost enclosing namespace scope, the block scope declaration declares that same entity and
receives the linkage of the previous declaration. If there is more than one such matching entity, the program is ill-formed.
Otherwise, if no matching entity is found, the block scope entity receives external linkage.[ Example:

There are three objects named i in this program. The object with internal linkage introduced by the declaration in
global scope (line //1 ), the object with automatic storage duration and no linkage introduced by the declaration on line
//2, and the object with static storage duration and external linkage introduced by the declaration on line //3. —end
example ]"

On the basis of the above explanation, I would have expected the object declared in line //3 to be the same one that is defined in line 1//. Can anyone enlighten me as to why it isn't? Also, if they are two different variables, how would you distinguish between them in this translation unit?

It is. External in this case means external to block scope. Notice that extern int i; is within a block, while the int i; that it links to is declared otuside tha block, hence it is external (to the block) linkeage. This is part of the standard to resolve any ambiguities, the example code is extremely poor code to begin with, the standard just defines how it must be dealt with, since it isnt exactly incorrect. The C/C++ standard is a lot like the national electrical code. 99% of it may seem silly because it deals with things that will mostly never come up, but it is there to deal with those oddball situations when they arrise, so there are no amiguities concerning what is and is not proper.

Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

which seems roughly analogous to the code in the standard, doesn't compile -- no suitable extern declaration for the third i is found.

I would guess the keyword is "visible"; the static int i declaration is not visible, i.e., not in scope (the declaration of the automatic variable removes the entire block from the scope of the static object), and extern must refer to a variable with linkage, and automatic variables have no linkage.

Well, that's interesting. It compiles and links on my Windows machine, but I cannot get my Mac (with gcc 4.0 and gcc 4.2) to link this program. extern int i is just a declaration, not a definition, so that can't get us into trouble.