假设我有以下 C11 程序:
#include <stdatomic.h>
int
main()
{
int x = 0; // same behaviour if `atomic_int x`, surprisingly
atomic_int y = (atomic_int) x;
}
Run Code Online (Sandbox Code Playgroud)
如果我用gcc
9编译它,我就不会出错。如果我用 Apple clang
11编译它,我会得到:
foo.c:7:17: error: used type 'atomic_int' (aka '_Atomic(int)') where arithmetic or
pointer type is required
atomic_int y = (atomic_int) x;
Run Code Online (Sandbox Code Playgroud)
在各种平台上使用各种Godbolt版本的 GCC、Clang 和其他支持 C11 的编译器时,类似的问题比比皆是。无论是否--std=c11
设置,都会发生这种情况。
快速阅读C11 标准草案(在 6.5.4 中)“类型名称应指定原子、限定或非限定标量类型,并且操作数应具有标量类型。”,这表明强制转换是合法的 - 我看不到任何其他说法。
所以,我的问题是:clang
这里有问题吗?其他编译器是否错误地接受了这个转换?这是允许clang
和gcc
允许不同的地方吗?
(顺便说一句,更广泛的上下文是,我正在使用生成 C 代码的第三方程序,其中包括从intmax_t
to 的转换atomic_int
。此转换适用于gcc
,这是该程序唯一支持的编译器,但是在clang
--- 上失败,因此,macOS 的gcc
,它是 的别名clang
。我正在尝试找出错误所在 ---clang
用于拒绝有效代码、用于生成无效代码的程序或用于假装编译器的 macOS不是替代品gcc
。)
归档时间: |
|
查看次数: |
118 次 |
最近记录: |