3
votes

6.7.4 specifies as a constraint that:

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.

So that would imply that in:

#if 1 /*OK*/
int const const_global=0;
inline int ret_const_global(void) { return const_global; }
int nonconst_global=0;
inline int ret_nonconst_global(void) { return nonconst_global; }
void noop(void) { static int const const_local_static = 42;  }
#else
//constraint violations
static int const const_global=0;
inline int ret_const_global(void) { return const_global; }
static int nonconst_global=0;
inline int ret_nonconst_global(void) { return nonconst_global; }
#endif

the #if 1 block is OK, while the other isn't.

What I don't quite get is what the part about "a reference to an identifier with internal linkage" is supposed to mean.

Is mentioning an otherwise OK static "a reference to an identifier with internal linkage"?

inline int ret_const_local_static(void) { 
    static int const const_local_static = 42; 
    return const_local_static; //compiles but OK?
}

is taking an address of a static it?

inline int const* ret_ref_to_const_local_static(void) { 
    static int const const_local_static = 42; 
    static int const*const ref = &const_local_static; 
    return ref;
}

None of my compilers are issuing diagnostics for the last two examples (and I'd particularly like to use the last one, despite the fact that I might get different addresses in different compilation units if the inline version is used by the compiler) but are they conforming?

What is "a reference to an identifier with internal linkage" supposed to mean?

1
Hopefully my last language-lawyer question on non-static inlines today. :DPSkocik
6.2.2p6 says "The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern." So I'd say the examples you want to use do not contain references to identifiers with internal linkage.Ian Abbott
@IanAbbott you could post that as an answer.M.M
"reference to an identifier" sounds like weasel words to me, I would guess they meant "identifier or some related shenanigans equivalent to using the identifier" but a more precise definition would have to wait for someone to concoct a defective example.M.M

1 Answers

3
votes

6.2.2p6 says:

The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern.

So I'd say the examples you want to use do not contain references to identifiers with internal linkage. They contain references to identifiers with no linkage.