Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c++ Subject: Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? Date: Mon, 15 Aug 2022 14:36:09 -0700 Organization: A noiseless patient Spider Lines: 47 Message-ID: <86fshxur3q.fsf@linuxsc.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="9f5fdd80571645ef69adbce5a0ea0f3b"; logging-data="4009956"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18b6n/r+p9p3L/wIILhi8sqJgOBVUj5bIs=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:k9d8E5YYO81Z312II/bN7OnV6A0= sha1:mJ7w0tQeXnLzXlxoThtFfN4rRik= Xref: csiph.com comp.lang.c++:85950 Andrey Tarasevich writes: > Sound like a silly question (and it might just as well be), but > nevertheless: > > int *a = ; > bool b = true; > a + b; > > How does the binary `+` work in this case? Its operands are an `int *` > and a `bool`. `bool` is an integer type, so the requirements are met > and the expression is valid. Naturally we expect that `bool` to be > promoted to an integer value of 1, and the rest is clear. > > But where does it say in the standard that `bool` should get promoted > in this context? There is no global rule requiring an unconditional > promotion, i.e. each operator's specification mandates integral > promotions individually and explicitly. And I don't see integral > promotions mentioned anywhere in [expr.add]. > > It does mention usual arithmetic conversions (UAC), of course, and UAC > include integral promotions. But UAC are only applicable when _both_ > operands have enumeration or arithmetic type. UAC are not applied when > one operand is a pointer. (The definition of UAC does not accommodate > for such possibility.) > > So, how does pointer+bool addition work then? When does `true` turn > into 1 here? Just a few comments.. One, I think you are raising a good point. Two, my guess is that this issue reflects an oversight on the part of the C++ standards committee. At the very least, assuming there is a provision in the C++ standard the gives this result, the reasoning needed is obscure and deserves a note. Three, an argument could be made (for C++20, n4860) that converting 'true' or 'false' to 1 or 0 is a consequence of the last sentence of section 7.3 paragraph 1: A standard conversion sequence will be applied to an expression if necessary to convert it to a required destination type. Granted, the reasoning is fraught with ambiguity, and that is never good. But the 'if necessary' provision gives a lot of latitude.