对具有外部链接的内联函数中的静态对象的引用

References to static objects in inline functions with external linkage

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.

所以这意味着在:

#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

#if 1 块可以,而另一个则不行。

我不太明白 "a reference to an identifier with internal linkage" 部分的意思。

是否提到了其他方面的 OK 静态 "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?
}

是取静态地址吗?

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 我的编译器正在为最后两个示例发出诊断(我特别想使用最后一个,尽管如果内联我可能会在不同的编译单元中获得不同的地址版本由编译器使用)但它们是否符合要求?

"a reference to an identifier with internal linkage" 是什么意思?

6.2.2p6 说:

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.

所以我想说您要使用的示例不包含对具有内部链接的标识符的引用。它们包含对没有链接的标识符的引用。