Skip to content
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

placement new inout issue #21160

Open
TurkeyMan opened this issue Apr 6, 2025 · 5 comments
Open

placement new inout issue #21160

TurkeyMan opened this issue Apr 6, 2025 · 5 comments
Assignees

Comments

@TurkeyMan
Copy link
Contributor

class C
{
    this(int) inout;
}
void main()
{
    void[__traits(classInstanceSize, C)] instance = void;
    C c = new(instance) C(10);
}
error : `inout` constructor `C.this` creates inout object, not mutable

This error seems wrong; the constructor signature doesn't determine the cost-ness of the constructed object; the argument does (in this case, it's a C)
Think this should work. It is conventional for constructors to be inout.

@TurkeyMan
Copy link
Contributor Author

@WalterBright I think this is a bug?

@WalterBright
Copy link
Member

An inout object cannot be modified, but you're assigning it to a mutable c. The inout applies to the this reference, which is the object being constructed.

The compiler is behaving correctly.

@TurkeyMan
Copy link
Contributor Author

The input is mutable C, so shouldn't the output be mutable C? That's literally the point of inout... It should be const during the constructor, but it should resolve to match the const-ness of the input on return; that's the point no?

People always write inout constructors...

@TurkeyMan
Copy link
Contributor Author

We mark copy constructors inout routinely; in the event it copies a mutable thing, the resulting object is mutable... no?

@TurkeyMan
Copy link
Contributor Author

Okay, I just realised that this works:

class C
{
    this(inout int) inout;  // const-ness matches SECOND arg
}
void test()
{
    C c1 = new C(10); // `10` is mutable so `this` is mutable
}

Obviously inout needs a point of reference, and there was no point of reference in my sample.
Sorry for the noise.

This is interesting to think about though; we find inout particularly useful for copy constructors (where the point of reference is the object we're copying from), but it seems there's no similar way to have a normal constructor apply to any const-qual state... we need to implement each individual qualified constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants