Clang为什么要抱怨“初始化元素”不是编译时常数。对于本地复合字面意思?

发布于 2025-02-06 19:38:57 字数 366 浏览 2 评论 0原文

使用以下代码,

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花坊 2025-02-13 19:38:57

在问题下的评论中,有人提到这是一个已知的错误,“这是一个旧的错误仍然无法修复” 。因此,在修复之前,您必须围绕它进行工作。

我可以找到触发器的最简单代码是错误是

int x = 3;
struct { __typeof__((int){x}) y; };

触发该错误是触发的:当以下内容:

  • 具有非构恒定初始化的化合物字面形式,
  • 在参数中使用
  • __ typeof __ typeof __在任何一个在任何一个中声明成员变量的声明struct或A Union

reintf2ii2f中使用宏函数,它不会触发错误,因为参数x不是复合文字。这些功能中的x是具有原始类型的变量。

但是在f2i2f函数中,参数x传递给 ofter reint的调用包含复合文字。该复合文字是参数的一部分,用于__ type __,它用于定义 ofter union的定义。

因此,解决方法是避免嵌套reint宏。例如,f2i2f可以实现如下:

float f2i2f(float x) {
    int temp = REINT(int, x);
    return REINT(float, temp);
}

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

int x = 3;
struct { __typeof__((int){x}) y; };

The bug is triggered when:

  • a compound literal with a non-constant initializer
  • is used in the argument to __typeof__
  • in the declaration of a member variable in either a struct or a union

When the REINT macro is used in the f2i and i2f functions, it doesn't trigger the bug because argument X is not a compound literal. X in those functions is a variable with a primitive type.

But in the f2i2f function, the argument X passed to the outer invocation of REINT contains a compound literal. And that compound literal is part of the argument to a __typeof__ that is used in the definition of the outer union.

So the workaround is to avoid nesting the REINT macro. For example, f2i2f can be implemented as follows:

float f2i2f(float x) {
    int temp = REINT(int, x);
    return REINT(float, temp);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文