Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c++
Subject: Re: C++20 concepts rocks
Date: Fri, 04 Feb 2022 23:37:01 -0800
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <86a6f5lq76.fsf@linuxsc.com>
References: <87k0easj5d.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="fd296145b453fec571e4c44d6840aea3"; logging-data="758"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/6KN7GTAD4HnllW11elyqDwt+1vQT/6EM="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:wTc+QSetSodxnKu4U4a0ev6/Rmo= sha1:M2SfE8dSbdv8LqMwYHMRTFS/lrs=
Xref: csiph.com comp.lang.c++:82907
"Alf P. Steinbach" writes:
> On 4 Feb 2022 17:15, Ben Bacarisse wrote:
[...]
>> template
>> requires is_same_v> basic_string> typename StringType::traits_type,
>> typename StringType::allocator_type>>
>> StringType formatClockCycles(uint64_t clockCycles)
>> {
>> char result[28], *ep = (&result)[1];
>> do {
>> sprintf(ep - 4, "%03lu", clockCycles % 1000);
>> if (ep != (&result)[1]) ep[-1] = '.';
>> ep -= 4;
>> } while (clockCycles /= 1000);
>> while (*ep == '0' && ep[1]) ep++;
>> return ep;
>> }
>>
>> (the appropriate comment on the 28 is left as an exercise to the reader!)
>
> Nice, but I believe it's formal UB to move a pointer to `char` from
> beyond an array into the array, and dereference it.
>
> The C committee wrote a rationale for their earlier decision to make
> it so, instead of admitting that it was bludner. Uh, blunder. I
> don't recall who provided that information, but someone in this
> group.
I suspect that you either are misremembering or have misunderstood.
Usage patterns like that seen above are defined behavior in C,
because they were in common use for at least 10 years before the
first C standard. It would be unthinkable for WG14 to have turned
it into undefined behavior.