C2x:如果 _Alignof(array type) 产生元素类型的对齐,那么允许 _Alignof(incomplete array type) 有用吗?

发布于 2025-01-12 10:30:50 字数 615 浏览 0 评论 0原文

此代码无效:

int x = _Alignof(int[]);

注意:GCC 生成:

error: invalid application of '__alignof__' to incomplete type 'int[]'

而 Clang 生成:

<nothing>

Per Semantics(添加强调):

当应用于数组类型时,结果是元素类型的对齐要求。

然而,为了满足元素类型的对齐要求,不需要大小。

因此,通过更改约束来使上面的代码有效是否有用

_Alignof 运算符不得应用于函数类型或不完整类型。

至(强调):

_Alignof 运算符不得应用于函数类型或不完整的非数组类型。

This code is invalid:

int x = _Alignof(int[]);

Note: GCC produces:

error: invalid application of '__alignof__' to incomplete type 'int[]'

while Clang produces:

<nothing>

Per Semantics (emphasis added):

When applied to an array type, the result is the alignment requirement of the element type.

However, in order to yield the alignment requirement of the element type, the size is not needed.

Hence, will it be useful to make the code above valid by changing the constraint

from:

The _Alignof operator shall not be applied to a function type or an incomplete type.

to (emphasis added):

The _Alignof operator shall not be applied to a function type or an incomplete non-array type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

逆蝶 2025-01-19 10:30:50

C2x:如果_Alignof(数组类型)产生元素类型的对齐,那么允许_Alignof(不完整数组类型)有用吗?

正如您所观察到的,语言规范明确禁止将 _Alignof 运算符应用于不完整类型的表达式或此类类型的括号名称。这是一种语言约束,因此实现诊断违规是一种一致性要求,从这个意义上说,Clang 的行为是不合格的。

另一方面,当所讨论的类型是具有完整元素类型但未指定元素数量的数组类型时,您有机会实现完全一致的 _Alignof 行为,这是正确的。数组中元素的数量并不是其对齐要求的一个因素,因此只要规范允许,这个操作就可以根据普通的_Alignof语义来解决。

它有用吗?有用吗?我确信这取决于人们考虑的可能用途范围。我不认为 _Alignof 总体上非常有用,尽管它确实有它的时刻。而且由于您可以直接从数组类型的元素类型获取数组类型的对齐方式,因此您不需要对数组类型使用 _Alignof ,无论完整与否,除非在您不知道什么的地方类型将被操作。这并没有排除所有可能的用途,但它留下的窗口足够小,我认为规范表达它所执行的更简单的约束而不是更复杂的约束并不是不合理的。需要允许 _Alignof 处理这一特殊的不完整类型。

C2x: if _Alignof(array type) yields the alignment of the element type, then will it be useful to permit _Alignof(incomplete array type)?

As you observe, the language spec explicitly forbids applying the _Alignof operator to an expression of incomplete type or to the parenthesized name of such a type. This is a language constraint, so it is a conformance requirement that implementations diagnose violations, and in this sense, Clang's behavior is non-conforming.

On the other hand, you're right that there is an opportunity for fully consistent _Alignof behavior when the type in question is an array type with complete element type but unspecified number of elements. The number of elements in an array is not a factor in its alignment requirement, so this operation could be resolved according to ordinary _Alignof semantics, provided only that the spec allowed it.

Would it be useful? I'm sure that depends on what range of possible uses one considers. I don't find _Alignof very useful in general, though it does have its moments. And since you can get the alignment of an array type directly from its element type, you don't need _Alignof for array types -- complete or not -- except in places where you don't know what type will be operated upon. That doesn't cut out all possible uses, but the window it leaves is small enough that I don't think it unreasonable for the spec to express the simpler constraint it does instead of the more complicated one that would be required to allow _Alignof on this one special class of incomplete types.

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