Manfred <non...@add.invalid> writes:
> On 6/16/2020 5:29 PM, Paavo Helde wrote:
>
>> 16.06.2020 15:26 Manfred kirjutas:
>>
>>> Cast operators are explicitly meant to tell the compiler: look,
>>> this pointer, irrespective of where it comes from, is a pointer to
>>> T, deal with it.
>>
>> The proposal to legislate malloc and friends for object creation
>> "
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p0593r6.html"
>> contains explicit remark, stressing the opposite:
>>
>> "Note that a pointer reinterpret_cast is not considered sufficient
>> to trigger implicit object creation."
>>
>> I gather all this fuss is about allowing type-based alias
>> analysis. Reinterpret_cast works directly against this idea.
>
> That's why I find the proposal going in the direction of causing more
> trouble than help.
> True, reinterpret_cast breaks type-based alias analysis. However, it
> has been part of the standard since day one, and since you can't do
> type analysis on a buffer returned by malloc anyway, what's the
> problem with that?
>
> By the way, I managed to look into Bjarne's book (p. 1260):
> "Note that malloc() etc. does not invoke constructors and free()
> doesn't invoke destructors. Do not use these functions for types with
> constructors and destructors. Aldo, memset() should never be used for
> any type with a constructor".
>
> That makes perfectly sense.
> I wonder why the standard managed to come up with something that
> denies /any/ object allocation with malloc (at least according to the
> first example in the proposal).
Having now reviewed the linked document, I will offer a few
remarks and opinions.
First I think the given motivating example (with malloc()) is
wrong. Per basic.life (6.8 p1) the lifetime of an object has
started. Per c.malloc (23.10.11 p1,p2) the semantics of a call
to malloc() is specified by the C standard library. Per the
description of "Memory management functions" (7.22.3) in the
referenced C standard (which is C 2011), a (non-null) pointer
returned by malloc() or calloc() or realloc() "may be assigned to
a pointer to any type of object [not having an extended alignment
requirement] and then used to access such an object or an array
of such objects in the space allocated".
That last part seems pretty airtight. Whatever we might imagine
is implied by other parts of the C++ standard, given an explicit
and specific statement that such pointers may be used to access
objects or arrays of objects, it seem reasonable to conclude that
those pointers are indeed so usable. Indexing operations and
pointer arithmetic is part and parcel of that, those being
necessary to carry out the accessing of array elements (which
doesn't matter to the paper's example but seems worth pointing
out).
Second, notwithstanding the first comment, the proposal takes
what I think is a good direction in suggesting that (for most of
it) the topic be treated as a Defect Report, not changing the
meaning of the language but only correcting an oversight in
previous writings. So for what we thought was working all along
it is admitted that indeed it was meant to work all along, and
what is changing is only how the Standard is worded, not the
C++ language.
Third, forgive me for saying this so bluntly, but the writing in
the C++ standard is pretty awful, and unfortunately the paper's
proposed changes don't show any signs that that is improving.
Fourth, and probably most important, regarding TBAA. I see the
motivation for doing TBAA, and in fact I agree with it, but I
think the general approach C++ is taking to accommodate TBAA is
misguided. Little by little, C++ is building up an elaborate
and complicated set of rules, with an unfortunate consequence:
If your program does almost anything at all outside of
vanilla language usage, and the optimizer f**** you, then
it's your fault; or, instead it might be that
The compiler/optimizer screwed up, either in understanding
or in implementing the rules (but unfortunately it is very
hard to be sure, and even harder to convince the compiler
writers of the truth in many cases).
I'm sorry to say that the "implicit lifetime" proposal continues
this trend.
(Editorial comment: does anyone else find it funny that the
set of changes proposed gives so many explicit statements about
so-called "implicit" properties? After all that it hardly
seems right to call them "implicit".)