www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21586] New: Struct dtor is called twice if struct is created

https://issues.dlang.org/show_bug.cgi?id=21586

          Issue ID: 21586
           Summary: Struct dtor is called twice if struct is created
                    inside ternary operator
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: temtaime gmail.com

import std.stdio;

struct S
{
        this(int arg)
        {
                a = arg;
                writeln(`this`);
        }

    ~this()
    {
        writeln(`~this `, a);
    }

        int a;
}

void main()
{
        auto s = true ? S(1) : S(0);
}

Outputs:
this
~this 1
~this 1

Should:
this
~this 1

--
Jan 26 2021