gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:17:49 26 4
gpt4 key购买 nike

6.7.4指定为约束 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 block 可以,而另一个则不行。

我不太明白“对具有内部链接的标识符的引用”部分的含义。

是否提到了一个其他方面还不错的静态“对具有内部链接的标识符的引用”?

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;
}

我的编译器都没有为最后两个示例发出诊断(我特别想使用最后一个,尽管如果编译器使用内联版本,我可能会在不同的编译单元中获得不同的地址) 但它们是否合规?

“对具有内部链接的标识符的引用”应该是什么意思?

最佳答案

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.

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

关于c - 对具有外部链接的内联函数中的静态对象的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53043389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com