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.
+
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