that is bug?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Apr 7 14:43:53 UTC 2018


On Saturday, April 07, 2018 14:29:15 kdevel via Digitalmars-d wrote:
> On Saturday, 7 April 2018 at 10:25:19 UTC, bauss wrote:
> > On Saturday, 7 April 2018 at 09:07:48 UTC, sdvcn wrote:
> >>         true?stt="AA":stt="BB";    <<<<-----///Out:BB
> >
> > It's an UB.
> >
> > Not a bug.
>
> Why UB? stt is only modified once.

It's modified twice. This

    import std.stdio;

    struct S
    {
        S opAssign(string str)
        {
            writeln(str);
            return S.init;
        }
    }

    void main()
    {
        S stt;
        true ? stt = "AA" : stt = "BB";
    }

prints

AA
BB

whereas this

    import std.stdio;

    struct S
    {
        S opAssign(string str)
        {
            writeln(str);
            return S.init;
        }
    }

    void main()
    {
        S stt;
        true ? (stt = "AA") : (stt = "BB");
    }

prints

AA

However, similar code in C++

    #include <stdio.h>

    class C
    {
    public:
        C& operator=(int i)
        {
            printf("%d\n", i);
            return *this;
        }
    };

    int main()
    {
        C c;
        true ? c = 42 : c = 29;
        return 0;
    }

prints the first value only, which would imply (though not guarantee) that
the D behavior is a bug.

- Jonathan M Davis



More information about the Digitalmars-d mailing list