aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMartin Sebor <msebor@redhat.com>2019-01-31 02:33:58 +0000
committerMartin Sebor <msebor@gcc.gnu.org>2019-01-30 19:33:58 -0700
commitf7478e4e33252da20e97138c7f1503e7706f300b (patch)
treef02425e77f343e37f99fc34e37fa07d6eba91ee6 /gcc
parent15b77e7407810879c20187d26c3781b9f171d86f (diff)
downloadgcc-f7478e4e33252da20e97138c7f1503e7706f300b.zip
gcc-f7478e4e33252da20e97138c7f1503e7706f300b.tar.gz
gcc-f7478e4e33252da20e97138c7f1503e7706f300b.tar.bz2
PR other/89106 - cast-to-union documentation incorrect w.r.t. lvalueness
gcc/ChangeLog: PR other/89106 * doc/extend.texi (cast to a union): Correct and expand. From-SVN: r268411
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog5
-rw-r--r--gcc/doc/extend.texi37
2 files changed, 33 insertions, 9 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 895194f..8b632f5 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2019-01-30 Martin Sebor <msebor@redhat.com>
+
+ PR other/89106
+ * doc/extend.texi (cast to a union): Correct and expand.
+
2019-01-30 Vladimir Makarov <vmakarov@redhat.com>
PR rtl-optimization/87246
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index f41c7a8..b51f427 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -2275,27 +2275,46 @@ case 1...5:
@cindex cast to a union
@cindex union, casting to a
-A cast to union type looks similar to other casts, except that the type
-specified is a union type. You can specify the type either with the
-@code{union} keyword or with a @code{typedef} name that refers to
-a union. A cast to a union actually creates a compound literal and
-yields an lvalue, not an rvalue like true casts do.
+A cast to a union type is a C extension not available in C++. It looks
+just like ordinary casts with the constraint that the type specified is
+a union type. You can specify the type either with the @code{union}
+keyword or with a @code{typedef} name that refers to a union. The result
+of a cast to a union is a temporary rvalue of the union type with a member
+whose type matches that of the operand initialized to the value of
+the operand. The effect of a cast to a union is similar to a compound
+literal except that it yields an rvalue like standard casts do.
@xref{Compound Literals}.
-The types that may be cast to the union type are those of the members
-of the union. Thus, given the following union and variables:
+Expressions that may be cast to the union type are those whose type matches
+at least one of the members of the union. Thus, given the following union
+and variables:
@smallexample
union foo @{ int i; double d; @};
int x;
double y;
+union foo z;
@end smallexample
@noindent
-both @code{x} and @code{y} can be cast to type @code{union foo}.
+both @code{x} and @code{y} can be cast to type @code{union foo} and
+the following assignments
+@smallexample
+ z = (union foo) x;
+ z = (union foo) y;
+@end smallexample
+are shorthand equivalents of these
+@smallexample
+ z = (union foo) @{ .i = x @};
+ z = (union foo) @{ .d = y @};
+@end smallexample
+
+However, @code{(union foo) FLT_MAX;} is not a valid cast because the union
+has no member of type @code{float}.
Using the cast as the right-hand side of an assignment to a variable of
-union type is equivalent to storing in a member of the union:
+union type is equivalent to storing in a member of the union with
+the same type
@smallexample
union foo u;