C++ Logo

std-proposals

Advanced search

[std-proposals] C99 inline and statics (was: Drop same sequence of tokens for inline)

From: Alejandro Colomar <alx.manpages_at_[hidden]>
Date: Fri, 5 May 2023 21:24:20 +0200
Hi David,

On 5/5/23 17:49, David Brown via Std-Proposals wrote:
> On 05/05/2023 16:04, Alejandro Colomar via Std-Proposals wrote:
>> Hi Thiago,
>>
>> On 5/5/23 03:22, Thiago Macieira via Std-Proposals wrote:
>>> On Thursday, 4 May 2023 17:54:12 PDT Alejandro Colomar wrote:
>>>> I still don't understand how use of static within an inline function
>>>> is not UB. Maybe in C++ it's useful. In C, I never found a need for
>>>> 'static' within an inline function.
>>>
>>> Indeed, but I'd say that it's a consequence of how statics inside inline were
>>> defined in C. That removed their usefulness, therefore they don't get used. The
>>> warning also probably exists because people tried to use it and have run into
>>> issues.
>>
>> Actually, I'm very confused about static within inline in C, since it seems to
>> really be UB in one paragraph, and defined behavior a few paragraphs below:
>>
>> <https://port70.net/~nsz/c/c11/n1570.html#6.7.4p3>
>> An inline definition of a function with external linkage shall
>> not contain a definition of a modifiable object with static or
>> thread storage duration, and shall not contain a reference to
>> an identifier with internal linkage.
>>
>> From the above, it seems UB. But then paragraph 7 has a note:
>>
>> <https://port70.net/~nsz/c/c11/n1570.html#6.7.4p7>
>> [...] It is unspecified whether a call to the function uses
>> the inline definition or the external definition. 140)
>>
>> OK so far. Here's the note:
>>
>> <https://port70.net/~nsz/c/c11/n1570.html#note140>
>> Since an inline definition is distinct from the corresponding
>> external definition and from any other corresponding inline
>> definitions in other translation units, all corresponding
>> objects with static storage duration are also distinct in each
>> of the definitions.
>>
>> What? Now the behavior seems to be defined to be distinct in all definitions
>> instead of the "shall not" happen from 6.7.4p3. Still unspecified which one
>> gets called.
>
> You cannot have two definitions visible at the same time in a single
> translation unit. If you have an inline definition of the function, you
> cannot have a distinct external definition. You can write an inline
> definition, and then an extern declaration for the function (without a
> function body) - that makes the same body definition available as an
> extern non-inlined function. Alternatively, you can have a non-inline
> version defined in a different translation unit. In this case, the
> function definitions can be different, and /that/ version's body can use
> statics like a normal function.

Agree, that definition can use statics.

However, I'm not convinced that it's what the standard tries to say. It
says that


>> <https://port70.net/~nsz/c/c11/n1570.html#note140>
>> Since an inline definition is distinct from the corresponding
>> external definition and from any other corresponding inline
>> definitions in other translation units, *all corresponding
>> objects with static storage duration are also distinct in each
>> of the definitions.*

Since there can only be one external definition, there can only be one
static object definition, so it can't be distinct (or for that matter,
equal) to any other version, because there's no other static version
of such object.

I also thought it might be referring to static inline, but it's not,
because it explicitly talks about "corresponding external definition".
I'm thinking it may be a bug in the standard?

Cheers,
Alex

Received on 2023-05-05 19:24:24