这里有一个相关的“什么”问题:C11 grammar ambiguity between _Atomic type specifier and qualifier
我感兴趣的是为什么,因为 C11 基本原理尚未发布,这似乎不必要地复杂。
语法包括这两者(并解决了有利于 shift
ing (
而不是 reduce
的歧义(否则会是如果后跟 declarator
或 abstract-declarator
)),可从 declaration-specifiers
或 specifier-qualifier-list< 访问
:
atomic-type-specifier: _Atomic ( type-name )
type-qualifier: _Atomic
这导致以下代码:
int i1;
int (i2); // valid, same as i1 - usually seen in the context of pointer-to-function or pointer-to-array
int _Atomic a1;
int _Atomic (a2); // invalid
_Atomic (int) a3; // valid, same as a1
想法:
_Atomic
不得修改数组或函数。除非声明符周围有多余的括号,这意味着#define _Atomic(x) x _Atomic
是有效的(当然如果#define
关键字是合法的)。当它作为
限定符
出现时,_Atomic
与const
是相同的句法部分,C 程序员 已经习惯于将const
放在正确的一侧,所以不能为了“易于使用”。
最佳答案
基本原理可在 N1485 中找到:
The
_Atomic
keyword also can also be used in the form_Atomic(T)
, whereT
is a type, as a type specifier equivalent to_Atomic T
. Thus,_Atomic(T) x, y;
declaresx
andy
with the same type, even ifT
is a pointer type. This allows for trivial C++0x compatibility with a C++ only_Atomic(T)
macro definition asatomic<T>
.
关于c - C11 _Atomic 说明符与限定符语法不规则的基本原理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36381380/