Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: That's while I love C++ over C Date: Sat, 06 Nov 2021 13:27:27 -0700 Organization: None to speak of Lines: 87 Message-ID: <87fss9xc5s.fsf@nosuchdomain.example.com> References: <87r1btygxh.fsf@nosuchdomain.example.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="94ef091bbfb98893e007ef1406ee748c"; logging-data="16273"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8waA2aQVeMlVGvQmHX9Wm" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:eDos2ZLwqKvg8DRSVCzq+s2GdDo= sha1:iN5ua2i1Dh6taHFEH6hTPMmwRjU= Xref: csiph.com comp.lang.c++:82255 David Brown writes: > On 06/11/2021 06:46, Keith Thompson wrote: >> red floyd writes: [...] >>> As with everything else in C++, there's nothing wrong with the >>> conditional operator ... in the right place. The left hand side of >>> an assignment is NOT the right place. Using the result of a conditional >>> as an rvalue is just fine, I use it all the time. >> >> Why? >> >> Seriously, the C++ standard (unlike the C standard) is clear that a >> conditional expression can be an lvalue if its second and third operands >> are both lvalues of the same type (I'm oversimplifying the rules a bit). >> In other words, the language explicitly allows a conditional expression >> to be the LHS of an assignment. Why do you object to taking advantage >> of that feature? >> >> I don't think I knew about it before seeing this discussion, but now >> that I do, the meaning of >> >> (x == y ? a : b) = c; >> >> seems perfectly clear to me. The alternative: >> >> if (x == y) { >> a = c; >> } >> else { >> b = c; >> } >> >> is more verbose and could be problematic if c is a complicated >> expression; the risk is that a later maintainer might modify one >> instance of c and not the other. > > That is a poor argument, IMHO, as it is so easily solved : > > auto new_c = complicated_expression; > if (x == y) { > a = new_c; > } else { > b = new_c; > } To be clear, I respect your opinion. I just don't share it in this case. I don't see your replacement code as a "solution", because I don't see the shorter version as a problem. Expanding one line to six can make sense in some cases. I just don't see this as one of those cases. In my humble opinion, the biggest barrier to understanding (x == y ? a : b) = c; is knowing that a conditional expression can appear on the LHS of an assignment, i.e., that it can be an lvalue. Any C or C++ programmer should already understand that the LHS of an assignment is an expression that's evaluated to determine what object is to be assigned to. Once you realize that a conditional expression can be an lvalue, the meaning **IMHO** becomes obvious. Of course in real code you very probably wouldn't call the variables x, y, a, b, and c. With meaningful names, I suspect the intent of the code would be clearer. If I saw that line presented as C, my reaction would be that it's illegal, but *if it were legal* the meaning would be obvious. [...] > "Always write code as though the person who will maintain it is a > violent psychopath who knows where you live" (I've forgotten the source > of that quotation). Also assume they are not as clever or experienced > as you are. Apparently it originated in a post by John F. Woods, right here on comp.lang.c++ in 1991. https://groups.google.com/g/comp.lang.c++/c/rYCO5yn4lXw/m/oITtSkZOtoUJ?pli=1 I've always liked that quotation. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */