std::optional defaut constructor

Jonathan Wakely jwakely@redhat.com
Thu Jun 4 13:38:28 GMT 2020


On 04/06/20 13:41 +0200, Richard Biener via Libstdc++ wrote:
>On Thu, Jun 4, 2020 at 11:34 AM Ville Voutilainen via Gcc-patches
><gcc-patches@gcc.gnu.org> wrote:
>>
>> On Thu, 4 Jun 2020 at 11:53, Marc Glisse <marc.glisse@inria.fr> wrote:
>> >
>> > On Thu, 4 Jun 2020, Ville Voutilainen wrote:
>> >
>> > > On Thu, 4 Jun 2020 at 11:00, Marc Glisse <marc.glisse@inria.fr> wrote:
>> > >> Maybe create a buffer, fill it with some non-zero values (-1?), then call
>> > >> placement new, and read some value in the middle of the buffer, possibly
>> > >> with some protection against optimizations? Ah, no, actual constructors
>> > >> are fine, it is only the inlined initialization that happens with the
>> > >> defaulted constructor that zeroes things.
>> > >
>> > > The zero-init is part of value-initialization of a class type with a
>> > > defaulted default constructor, so value-initialization with placement
>> > > new should indeed show us whether the target buffer is zeroed.
>> >
>> > Ah, yes, I had forgotten the empty () at the end of the operator new line
>> > when testing. Now the patch makes this runtime test go from abort to
>> > success at -O0 (with optimizations, the memset is removed as dead code). I
>> > am still not sure we want this kind of test though. And I added launder
>> > more to quiet a warning than with confidence that it does the right thing.
>> >
>> > #include <optional>
>> > struct A {
>> >    int a[1024];
>> > };
>> > typedef std::optional<A> O;
>> > int main(){
>> >    unsigned char t[sizeof(O)];
>> >    __builtin_memset(t, -1, sizeof(t));
>> >    new(t)O();
>> >    if(std::launder(t)[512] != (unsigned char)(-1)) __builtin_abort();
>> > }
>>
>> Yeah, I think the patch is OK with or without the test. As a side
>> note, you don't need the launder
>> if the check uses the pointer value returned by placement-new.
>
>Doesn't the placement new make the memory state of anything
>not explicitely initialized indeterminate?  That is, isn't the
>testcase broken anyways since GCC can elide the memset
>when seeing the placement new?

Yes.

IIUC -fno-lifetime-dse means the constructor that the placement new
invokes doesn't clobber the old contents of the memory, but it seems
fragile to rely on that remaining true in the long term.



More information about the Gcc-patches mailing list