C++ Logo

std-discussion

Advanced search

Re: Is it valid use reinterpret_cast to form pointer to object?

From: Peter C++ <peter.cpp_at_[hidden]>
Date: Tue, 1 Aug 2023 12:13:12 +0200
imho: yes. without placement new there might not be an object there (modulo implicit lifetime starts that changed/extended in C++23.

Sent from Peter Sommerlad's iPad
+41 79 432 23 32

> On 1 Aug 2023, at 12:10, language.lawyer--- via Std-Discussion <std-discussion_at_[hidden]> wrote:
>
> 
>>
>> Can you clarify your question please? Are you asking about this example
>> alignas(T) std::byte storage[sizeof(T)];
>> T *ptr = ::new (&storage) T;
>> foo(reinterpret_cast<T*>(&storage));
>> or
>> alignas(T) std::byte storage[sizeof(T)];
>> foo(reinterpret_cast<T*>(&storage));
>
> Is there much difference, when `reinterpret_cast<T*>(&storage)` is considered?
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2023-08-01 10:13:25