New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make move CTFEable for simple structs and classes #2036
Conversation
// Most complicated case. Destroy whatever target had in it | ||
// and bitblast source over it | ||
static if (hasElaborateDestructor!T) typeid(T).destroy(&target); | ||
|
||
memcpy(&target, &source, T.sizeof); | ||
static if (hasElaborateCopyConstructor!T || hasElaborateDestructor!T | ||
|| !isAssignable!T) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You also need to test hasElaborateAssign
.
Once you test hasElaborateAssign
, I don't think there's any need to test for hasElaborateCopyConstructor!T
or hasElaborateDestructor!T
either. Just:
static if (hasElaborateAssign!T || !isAssignable!T)
In that order. Unless I missed something? It's what we do in the rest of phobos anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems you are right except for hasElaborateDestructor, since we must do memcpy to avoid calling destructor on void-initialized result
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are over thinking it. If anything cause a = *
to do anything other than a straight up bit copy, then a will have an elaborate assign, and be marked as such, and that will be the end of that.
struct S
{
~this(){}
}
void main()
{
static assert(hasElaborateAssign!S);
}
So I agree that "destructor" => "must do memcpy", but you are already covered by "destructor" => "elaborate assign".
@monarchdodra |
Yes, there are two separate operations, with two separate tests:
Dealing with destruction is indeed unchanged. The assignement test is new, so there's no "keeping things the same way they were originally". In any case, |
Well, I honestly do not care which combination of traits to use. |
LGTM, but needs a rebase now.
You or me? I tend to be not so good at writing documentation. |
memcpy is not CTFEable, so avoid it for structs without dtor, postblit that can be assigned with plain =. This leaves as is (compile error in CTFE) structs that contain immutable/const memebers the fact that moves overwrites such fields is questionable to begin with.
Done.
I'll take a shot at docs. |
Auto-merge toggled on |
Make move CTFEable for simple structs and classes
Thanks! |
memcpy is not CTFEable, so avoid it for structs without
dtor, postblit that also can be assigned with plain
lhs = rhs;
.This leaves as is (compile error in CTFE) structs that contain
immutable/const memebers the fact that moves overwrites such
fields is questionable to begin with.
Remark: pointsTo doesn't work at CTFE. The check itself makes no sense for class instances and primitive types.