Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #85950
| Path | csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
| 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> (permalink) |
| References | <tde0o3$3nqef$1@dont-email.me> |
| 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 |
Show key headers only | View raw
Andrey Tarasevich <andreytarasevich@hotmail.com> writes:
> Sound like a silly question (and it might just as well be), but
> nevertheless:
>
> int *a = <whatever>;
> 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.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
`bool` in pointer arithmetic: when does the promotion occur, if ever? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-08-15 10:45 -0700
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? David Brown <david.brown@hesbynett.no> - 2022-08-15 20:09 +0200
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-08-15 11:29 -0700
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-08-15 11:35 -0700
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? David Brown <david.brown@hesbynett.no> - 2022-08-15 21:20 +0200
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? red floyd <no.spam.here@its.invalid> - 2022-08-15 13:49 -0700
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-08-15 13:59 -0700
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? David Brown <david.brown@hesbynett.no> - 2022-08-16 09:12 +0200
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-08-16 09:56 -0700
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-15 07:18 -0700
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? Language Lawyer <language.lawyer@gmail.com> - 2022-09-19 03:28 -0700
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-10-02 12:37 -0700
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-08-15 14:36 -0700
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-15 14:57 -0700
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-08-16 21:13 +0200
Re: `bool` in pointer arithmetic: when does the promotion occur, if ever? Language Lawyer <language.lawyer@gmail.com> - 2022-08-17 09:08 -0700
csiph-web