admin 管理员组文章数量: 1086019
C23 added the typeof_unqual keyword as a way to get the unqualified type of an object. Can't you just use the + operator on scalar types to avoid this? (I know this doesn't work on struct/union typed objects). I get different results on different compilers with this.
int main() {
const int A;
typeof(+A) B;
B = 5;
return 0;
}
C23 added the typeof_unqual keyword as a way to get the unqualified type of an object. Can't you just use the + operator on scalar types to avoid this? (I know this doesn't work on struct/union typed objects). I get different results on different compilers with this.
int main() {
const int A;
typeof(+A) B;
B = 5;
return 0;
}
Share
Improve this question
edited Mar 27 at 12:12
Badasahog
asked Mar 27 at 10:09
BadasahogBadasahog
8714 silver badges20 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 5Can't you just use the + operator on scalar types to avoid this [for arithmetic types]?
Yes, except the integer promotions will be applied. C 2024 6.3.3.1 specifies that lvalue conversion (use of an lvalue in an expression for its value) removes qualifiers: “If the lvalue has qualified type, the value has the unqualified version of the type of the lvalue…”
An alternative that does not apply the integer promotions is typeof(0, A)
. This can also be used on non-arithmetic types.
I get different results on different compilers with this. [From a comment:] breaks on msvc.
Testing on Compiler Explorer shows MSVC 19.40 retains the const
qualifier in typeof(+A)
. MSVC does not conform to the C standard in this regard.
Further experimentation shows you can work around this with typeof(A+0)
. With typeof(0+A)
or typeof(x+a)
, where x
was declared int
, MSVC still produced a qualified type, but making A
the left operand of +
produces an unqualified type.
本文标签:
版权声明:本文标题:c - is using the + operator a way to remove qualifiers within the typeof operator instead of using typeof_unqual in C23? - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744097385a2533150.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
+
wouldn't work on pointer type objects, but you could use the binarytypeof(0+(A))
on those, and on other scalar types. (Or you could usetypeof(&*(A))
for pointer type objects.) – Ian Abbott Commented Mar 27 at 10:20short
that are narrower thanint
? – chux Commented Mar 27 at 13:44