Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Linkage (http://www.velocityreviews.com/forums/t956956-re-linkage.html)

Ike Naar 01-26-2013 06:52 AM

Re: Linkage
 
On 2013-01-26, Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
> Hi,
> In gcc-4.7 C99:
>
> extern int a;
> static int a;
>
> I get: error: static declaration of 'a' follows non-static declaration
>
>
> In WG14/N1256, 6.2.7 p4 seems to say this should not be an error.
>
>
> 6.2.2 p4:
>
> "For an identifier declared with the storage-class specifier extern in a scope
> in which a prior declaration of that identifier is visible,(23) if the prior
> declaration specifies internal or external linkage, the linkage of the
> identifier at the later declaration is the same as the linkage specified at the
> prior declaration. If no prior declaration is visible, or if the prior
> declaration specifies no linkage, then the identifier has external linkage."
>
>
> 6.2.2 p7:
>
> "If, within a translation unit, the same identifier appears with both internal
> and external linkage, the behavior is undefined."
>
>
> 6.2.7 p4:
>
> "For an identifier with internal or external linkage declared in a scope in
> which a prior declaration of that identifier is visible,(47) if the prior
> declaration specifies internal or external linkage, the type of the identifier
> at the later declaration becomes the composite type."


With

extern int a;
static int a;

the extern declaration is not in a scope in which a prior declaration
is visible (the static declaration appears later in the program).
So the identifier is declared with external linkage.
Then, when the static declaration appears, the identifier is declared
with internal linkage, and now the same identifier appears with
both internal and external linkage.

You can have both declarations if you change the order:

static int a;
extern int a;

in which case the identifier will have internal linkage.

Shao Miller 01-26-2013 04:05 PM

Re: Linkage
 
On 1/26/2013 02:12, Russell Shaw wrote:
>
> My mistake was seeing "composite" as combining "static" and "extern" as
> in 6.2.2 p4.
>


Storage-class specifiers are not part of a type. You might notice that
'typedef' is a storage-class specifier, too.

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter

James Kuyper 01-26-2013 06:16 PM

Re: Linkage
 
On 01/26/2013 02:12 AM, Russell Shaw wrote:
> On 26/01/13 17:52, Ike Naar wrote:
>> On 2013-01-26, Russell Shaw<rjshawN_o@s_pam.netspace.net.au> wrote:
>>> Hi,
>>> In gcc-4.7 C99:
>>>
>>> extern int a;
>>> static int a;
>>>
>>> I get: error: static declaration of 'a' follows non-static declaration
>>>
>>>
>>> In WG14/N1256, 6.2.7 p4 seems to say this should not be an error.
>>>
>>>
>>> 6.2.2 p4:
>>>
>>> "For an identifier declared with the storage-class specifier extern in a
>>> scope in which a prior declaration of that identifier is visible,(23) if
>>> the prior declaration specifies internal or external linkage, the linkage
>>> of the identifier at the later declaration is the same as the linkage
>>> specified at the prior declaration. If no prior declaration is visible, or
>>> if the prior declaration specifies no linkage, then the identifier has
>>> external linkage."
>>>
>>>
>>> 6.2.2 p7:
>>>
>>> "If, within a translation unit, the same identifier appears with both
>>> internal and external linkage, the behavior is undefined."
>>>
>>>
>>> 6.2.7 p4:
>>>
>>> "For an identifier with internal or external linkage declared in a scope
>>> in which a prior declaration of that identifier is visible,(47) if the
>>> prior declaration specifies internal or external linkage, the type of the
>>> identifier at the later declaration becomes the composite type."

>>
>> With
>>
>> extern int a;
>> static int a;
>>
>> the extern declaration is not in a scope in which a prior declaration
>> is visible (the static declaration appears later in the program). So the
>> identifier is declared with external linkage. Then, when the static
>> declaration appears, the identifier is declared with internal linkage, and
>> now the same identifier appears with both internal and external linkage.
>>
>> You can have both declarations if you change the order:
>>
>> static int a;
>> extern int a;
>>
>> in which case the identifier will have internal linkage.

>
> Hmm, so "becomes the composite type" really means apply the function
> "composite_type()", where composite_type() is a function containing 6.2.7 p3.
>
> My mistake was seeing "composite" as combining "static" and "extern" as in 6.2.2 p4.


Well, there was also a mistake involved if you thought that 6.2.2p4
applied to your example. When the extern declaration comes first,
6.2.2p4 doesn't apply, since the static declaration is not in scope yet,
but 6.2.2p7 does apply, rendering everything else irrelevant.

When the static declaration comes first, 6.2.2p4 applies, with the
result that 6.2.2p7 doesn't (since it has only internal linkage, the
'extern' keyword notwithstanding), thereby allowing 6.2.7p4 to be relevant.
--
James Kuyper


All times are GMT. The time now is 01:24 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.