Nota: the post I am talking about is this one.
While discussing on another question (here for reference), I got a link to that answer which had just been deleted. Here is its original content (I have edited it to remove the first sentence):
I can't comment, so I'll post my comment as an answer. The currently accepted answer is wrong. Compiler can't optimize to
return 42
.Even though
reinterpret_cast
from "pointer to an array element type"T*
to "pointer to the array type"T(*)[N]
will not give you a pointer to the array object, you canstd::launder
the result ofreinterpret_cast
and get the pointer to the array object (if it exists, of course). All the bytes of the immediately-enclosing array object of an array element are reachable through the pointer to the array element. (see http://eel.is/c++draft/ptr.launder).Given
p0 = &a.x[1]
,p1 = p0 - 1
points toa.x[0]
,p2 = std::launder(reinterpret_cast<double(*)[4]>(p1))
points toa.x
,p3 = reinterpret_cast<A*>(p2)
points toa
,p3->n = 88
changesa.n
andreturn 42
would be incorrect optimization.
Ok, it was written by a 1rep only user, is a late answer and starts with I can't comment so post as an answer.
The point is that it is indeed written as an acceptable answer:
- it is relevant for the question
- it contains interesting material
- it is far to long and detailed to be a simple comment
If it had not been deleted by a moderator, I would have edited it to remove the sentence (wrongly) stating it was a comment, and would have voted for undelete. Not that I really agree with that interpretation of the standard, but my opinion is still that it tries to answer the question.
Simply a post deleted by a diamond moderator cannot by undeleted by the community, hence this question.
Edit:
Even after removing the I can't comment... part, the post was still written as a comment. After BoltClock's advice I have edited it a bit further to first make it an answer, and then explain why the accepted answer should be wrong.
std::launder
can convert between a pointer to an array and a pointer to its first element which is an answer to the question. I understand that in its current wording the reader must find its way amont details only relative to another answer. Would it be acceptable that I edit the answer further to make it more explicit?std::launder
part which constitute a serious argument (it was upvoted before being deleted). I do not want to endorse that opinion, but I would find fair to allow the poster of that solution to present it. If comments were not disabled on deleted post, I would have pinged the author in a comment before that edit. Maybe I should ping him from a non deleted post like this one?