根据C11 § 6.5.2.3规定:
这是有道理的,因为一般情况下不能访问整个结构。但为什么
换句话说,将结构体标记为
Accessing a member of an atomic structure or union object results
in undefined behavior.
这是有道理的,因为一般情况下不能访问整个结构。但为什么
_Atomic
也是类型限定符而不仅仅是类型说明符呢?换句话说,将结构体标记为
_Atomic
有什么目的?我既不能读取也不能写入其中的任何元素。#include <stdatomic.h>
struct {
int x;
} _Atomic foo;
int main(void) {
foo.x = 42; // write error
return foo.x; // read error
}
在GCC/Clang中,foo.x
的两个访问都会导致警告/错误——这在C11标准中是完全没问题的。那么我为什么还要将结构体定义为_Atomic
限定符呢?