Storage-class specifiers and declarations

Hi there!

I try to understand the section 7.1.1.7 in C++ Standard:

The linkages implied by successive declarations for a given entity shall agree. That is, within a given scope, each declaration declaring the same object name or the same overloading of a function name shall imply the same linkage.


There are some examples below:

1
2
inline void h(); //internal linkage
void h();        //still internal linkage 


It's OK.

But I think that this one is wrong:

1
2
inline void m();
extern void m(); // external linkage 


1st declaration has internal linkage and the second one has external linkage !

This example is contrary to the section 7.1.1.7! These two declarations don't imply the same linkage...
Can anyone help me ?
Why should "inline void m();" have internal linkage? It has external linkage. (cp. 7.1.1.6) "m()" wasn't declared before, so it defaults to external.


Don't get confused with the inline. That's not a storage specifier.


1
2
inline void h(); //internal linkage
void h();        //still internal linkage 

That's not in my copy of the standard (C++03) and not true either. Maybe you are reading an old standard or draft, where inline was some magical static-similar thingie that used to confuse everyone?

Ciao, Imi.

BTW: I guess you were talking about the standard, and not specific compiler implementations, right? Not all compiler respect this paragraph. "extern int i; static int i;" works fine with cl.exe
Last edited on
I guess you were talking about the standard, and not specific compiler implementations, right?


Yes.

Why should "inline void m();" have internal linkage?


I thought it was obvious: beacuse of 'inline'...
Inline functions have internal linkage, haven't they ?
So ?
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/cplr243.htm#inline_linkage


In C, inline functions are treated by default as having static linkage; that is, they are only visible within a single translation unit. Therefore, in the following example, even though function foo is defined in exactly the same way, foo in file a.c and foo in file b.c are treated as separate functions: two function bodies are generated, and assigned two different addresses in memory:



Nevertheless, in C++, inline functions are treated by default as having external linkage, meaning that the program behaves as if there is only one copy of the function. The function will have the same address in all translation units and each translation unit will share any static locals and string literals. Therefore, compiling the previous example gives the following output:
Last edited on
Topic archived. No new replies allowed.