简体   繁体   中英

block scope extern declaration

The C++11 standard give the code snippet below (I deleted unrelated code) and said the name i have external linkage. (clause 3.5.6)

static int i = 0; // #1
void g() {
    extern int i; // #3 external linkage
}

Why do they do this? Did I misunderstand something? The two i refer to the same object in vs2012. And when I use i somewhere else, i got an unresolved external error. I have no idea whether vs2012 support this feature or not.

Edit: I think VS2012 is doing the right thing. The i in #3 only need to refers to an i that has a linkage. If the compiler can't find one, than the i should be defined in other translation unit. So the two i should refer to the same object in the code snippet above.

The quote from the standard:

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 no matching entity is found, the block scope entity receives external linkage.

But why people need this feature?

#3 is only a declaration; it states that a variable called i exists somewhere in the program, with external linkage, but does not define that variable. The declaration allows you to use that, rather than the static variable from #1 , within the scope of g .

You will also need to define it, in the namespace that contains g . In this case, it will have to be in a different translation unit, so that it doesn't conflict with the static variable with the same name.

To be clear, there are two different variables called i here, as explained in the paragraph following the example. #1 is defined here; #3 is only declared, and needs a separate definition.

static int i = 0; // #1
void g() {
    extern int i; // #3 external linkage
}

The first static i is a declaration and is visible only in the current source file.

extern int i; 

tells the compiler I don't mean this static i but another i defined somewhere else. If you haven't defined it somewhere else (in another translation unit) you will get the undefined reference.

And this doesn't break the ODR because this (the static ) i is static (visible only in this unit).

extern int i;

Promises compiler I will give you an int i .

The static int i=0; is not that promised variable, and you have to declare a int i somewhere else visible to that extern variable declaration.

In other words extern int i; and static int i=0; are two irrelevant variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM