Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #85950

Re: `bool` in pointer arithmetic: when does the promotion occur, if ever?

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 2022-08-15 14:36 -0700
Organization A noiseless patient Spider
Message-ID <86fshxur3q.fsf@linuxsc.com> (permalink)
References <tde0o3$3nqef$1@dont-email.me>

Show all headers | 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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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