Clang为什么要抱怨“初始化元素”不是编译时常数。对于本地复合字面意思?
使用以下代码,
#define REINT(T, X) (union {__typeof__(X) x; T t;}){X}.t
int f2i(float x) {
return REINT(int, x);
}
float i2f(int x) {
return REINT(float, x);
}
float f2i2f(float x) {
return REINT(float, REINT(int, x));
}
Clang抱怨f2i2f
“初始化器元素不是编译时常数”。这似乎很奇怪,因为复合文字“具有与 封闭块”(6.5.2.5p5),以及此类对象的初始化器列表中的元素不必是编译时常数。
With the following code,
#define REINT(T, X) (union {__typeof__(X) x; T t;}){X}.t
int f2i(float x) {
return REINT(int, x);
}
float i2f(int x) {
return REINT(float, x);
}
float f2i2f(float x) {
return REINT(float, REINT(int, x));
}
Clang complains about f2i2f
that an "initializer element is not a compile-time constant". This seems odd because compound literals "have automatic storage duration associated with
the enclosing block" (6.5.2.5p5), and the elements in the initializer list for such objects don't have to be compile-time constants.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在问题下的评论中,有人提到这是一个已知的错误,“这是一个旧的错误仍然无法修复” 。因此,在修复之前,您必须围绕它进行工作。
我可以找到触发器的最简单代码是错误是
触发该错误是触发的:当以下内容:
__ typeof __ typeof __
在任何一个在任何一个中声明成员变量的声明struct
或AUnion
当
reint
在f2i
和i2f
中使用宏函数,它不会触发错误,因为参数x
不是复合文字。这些功能中的x
是具有原始类型的变量。但是在
f2i2f
函数中,参数x
传递给 ofterreint
的调用包含复合文字。该复合文字是参数的一部分,用于__ type __
,它用于定义 ofterunion
的定义。因此,解决方法是避免嵌套
reint
宏。例如,f2i2f
可以实现如下:In the comments under the question, it was mentioned that this is a known bug, and "it is quite an old bug still not fixed". So until it's fixed, you'll have to work around it.
The simplest code I could find that triggers the bug is
The bug is triggered when:
__typeof__
struct
or aunion
When the
REINT
macro is used in thef2i
andi2f
functions, it doesn't trigger the bug because argumentX
is not a compound literal.X
in those functions is a variable with a primitive type.But in the
f2i2f
function, the argumentX
passed to the outer invocation ofREINT
contains a compound literal. And that compound literal is part of the argument to a__typeof__
that is used in the definition of the outerunion
.So the workaround is to avoid nesting the
REINT
macro. For example,f2i2f
can be implemented as follows: