Structs are Not Plain: A call for empty struct constructors

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Sep 20 06:01:00 UTC 2019


On Thursday, September 19, 2019 11:00:13 PM MDT Max Samukha via Digitalmars-
d wrote:
> On Thursday, 19 September 2019 at 20:25:53 UTC, Jonathan M Davis
>
> wrote:
> > with(Fixture.cons())
> > {
> > }
>
> That is UB. The spec does not state clearly, in which cases RVO
> will be applied. One may not assume that the destructor won't be
> called again on the already destructed object.

It's exactly the same as

with(Fixture(42))
{
}

would be or

with(Fixture())
{
}

would be if structs had default constructors. My point was that you can get
the same behavior as

with(Fixture())
{
}

right now by using a factory function. The syntax just ends up being a bit
more verbose. It was the OP who wanted to actually use with in this manner,
and if

with(Fixture.cons())
{
}

didn't work, then they could just do something like

auto f = Fixture.cons();
with(f)
{
}

The issue of whether with allows you to construct a temporary that's valid
for the entire scope of the with block is a separate issue from whether D
should be altered to have default constructors. The OP just happened to use
it in their example, and it does currently seem to work even if the spec is
silent on the matter. Whether it ever gets changed to not work or the spec
gets updated to define its behavior is irrelevant to the issue of default
constructors.

- Jonathan M Davis





More information about the Digitalmars-d mailing list