home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!asuvax!ncar!noao!arizona!optima.UUCP
- From: dwebster@optima.UUCP (Dave E. Webster Jr.)
- Newsgroups: comp.lang.c
- Subject: Re: a ? b : c = d; /* legal ? */
- Message-ID: <28578@optima.cs.arizona.edu>
- Date: 19 Dec 92 00:36:06 GMT
- References: <BzDA4u.459@cdsmn.mn.org>
- Sender: news@cs.arizona.edu
- Lines: 38
-
- Rich Wells asks:
-
- > Is the statement:
- >
- > a ? b : c = d;
- >
- > where a, b, c, and d are all integers, a valid C expression?
- ...
- > ***** Microsoft C (7.0 and 5.1): The statement does not compile.
- ...
- > ***** Turbo C++ (3.0): The statement is interpreted as:
- >
- > if (a)
- > a = d;
- > else
- > b = d;
-
- Rich,
-
- Assuming that the TC++ implementation should actually read as:
-
- if (a) b = d; else c = d;
- ^ ^
-
- then both of the above compilers are correct. The Microsoft compiler
- flags an error because the result of the trinary conditional in C is
- an rvalue and cannot be the target of an assignment. C++ changed the
- rules by defining the result of a trinary conditional as an lvalue of
- type T if (and only if) both the second and third arguments are
- themselves lvalues of a common type (cf arm 5.16).
-
- I have not seen this feature used much in C++ but other languages
- with similar semantics make great use of this and similar semantics
- (eg: Icon).
-
- Merry XMAS,
-
- Dave
-