int test()
{
int t0;
int* n0 = new int;
*n0 = t0;
struct S { int x; }
S t1;
S* n1 = new S;
*n1 = t1;
return 10;
}
pragma(msg, test());
The int assigns fine, the struct with an int member doesn't.
Is there a workaround?
Comment #1 by turkeyman — 2019-01-21T00:42:54Z
Error: `*n1 = t1` cannot be evaluated at compile time
Comment #2 by iamthewilsonator — 2019-01-21T03:33:08Z
*** Issue 19600 has been marked as a duplicate of this issue. ***
Comment #3 by simen.kjaras — 2019-01-21T07:13:21Z
Defining an opAssign for S seems to do the trick:
int test()
{
struct S { int x; void opAssign(S s) { x = s.x; } }
S t1;
S* n1 = new S;
*n1 = t1;
return 10;
}
pragma(msg, test());
Comment #4 by turkeyman — 2019-01-21T07:25:56Z
This also works:
(*n1).tupleof = t1.tupleof
The compiler can do it... it's just that this expression doesn't work.
Comment #5 by simen.kjaras — 2019-01-21T07:34:23Z
Workaround #2:
int test2()
{
struct S { int x; }
S t1;
S* n1 = &(new S[1])[0];
*n1 = t1;
return 10;
}
pragma(msg, test2());
Something gets weird when using `new S` - it seems the allocated block is marked in some way that prevents *m1 = t1 from working.
Comment #6 by turkeyman — 2019-01-21T08:58:31Z
I also thought of and found that workaround... it's fine for arrays. It's very strange that because it's just one, this doesn't work.
Comment #7 by simen.kjaras — 2019-01-21T12:58:57Z
It seems this can be fixed by teaching assignToLvalue about StructLiteralExps:
else if (auto sle = e1.isStructLiteralExp())
{
oldval = sle;
}
There may be issues with this fix though.
Comment #8 by robert.schadek — 2024-12-13T19:02:05Z