]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
doc: type-punning through a union is a GNU extension for C++
authorSam James <sam@gentoo.org>
Thu, 9 Oct 2025 02:40:58 +0000 (03:40 +0100)
committerSam James <sam@gentoo.org>
Thu, 9 Oct 2025 03:11:48 +0000 (04:11 +0100)
We didn't explicitly say that type-punning through a union is undefined
behavior in C++. Mention that, and that we support it as a GNU extension.

This was reported on LLVM's Discourse (forums) [0].

[0] https://discourse.llvm.org/t/ub-when-type-punning-through-unions/88527/6

gcc/ChangeLog:
PR c++/117219

* doc/invoke.texi (-fstrict-aliasing): Explain that type-punning
through a union in C++ is supported as a GNU extension.

gcc/doc/invoke.texi

index 41ce025350e7ebac1ea459de6a98a814a556ff83..c0d1442f7b19eb1f910c88c786451a9582ccf7c3 100644 (file)
@@ -15049,10 +15049,11 @@ int f() @{
 @end smallexample
 The practice of reading from a different union member than the one most
 recently written to (called ``type-punning'') is common.  Even with
-@option{-fstrict-aliasing}, type-punning is allowed, provided the memory
-is accessed through the union type.  So, the code above works as
-expected.  @xref{Structures unions enumerations and bit-fields
-implementation}.  However, this code might not:
+@option{-fstrict-aliasing}, type-punning is allowed in C, provided the memory
+is accessed through the union type.  In ISO C++, type-punning through a union
+type is undefined behavior, but GCC supports it as an extension. So, the code
+above works as expected.  @xref{Structures unions enumerations and
+bit-fields implementation}.  However, this code might not:
 @smallexample
 int f() @{
   union a_union t;