Re: Struct copy constructor with inout

2023-11-16 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 13:58:17 UTC, Paul Backus wrote: On Tuesday, 14 November 2023 at 13:41:32 UTC, Steven Schveighoffer wrote: ``` Error: copy constructor `testinoutctor.S1.this(ref const(S1) s) const` is not callable using argument types `(const(S1))` ``` I'm not sure what this

Re: Struct copy constructor with inout

2023-11-14 Thread dhs via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 16:51:07 UTC, Paul Backus wrote: There's no assignment. The value is constructed in-place, in `ss2`'s memory. The reason the compiler allows you to construct a `const(S2)` value inside of an `S2` variable is that `const(S2)` implicitly converts to `S2`.

Re: Struct copy constructor with inout

2023-11-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 13:58:17 UTC, Paul Backus wrote: It's easier to see if you compare the actual and expected argument lists side-by-side Expected: (ref const(S1) s) const Actual: (const(S1) ) ^ Mismatched

Re: Struct copy constructor with inout

2023-11-14 Thread dhs via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 14:58:21 UTC, Paul Backus wrote: ```d struct S2 { int* p; this(const int* p) const { // Ok - counts as initialization this.p = p; } } immutable int answer = 42; void main() { S2 s2; // If this were allowed to compile...

Re: Struct copy constructor with inout

2023-11-14 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 14:36:57 UTC, dhs wrote: Just to clarify some more: isn't "s1 = ss1" similar to something like: ```d const(S1) s1; S1 ss1; // ss1 is now S1.init S1_copy_construct_const_in_const_out(ss1, s1); ``` If this is the case, the compile error is expected,

Re: Struct copy constructor with inout

2023-11-14 Thread dhs via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 14:36:57 UTC, dhs wrote: Just to clarify some more: isn't "s1 = ss1" similar to I meant "ss1 = s1" here, sorry.

Re: Struct copy constructor with inout

2023-11-14 Thread dhs via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 13:58:17 UTC, Paul Backus wrote: On Tuesday, 14 November 2023 at 13:41:32 UTC, Steven The error is saying that the copy constructor expects a `const` `this` argument, but you're passing a mutable `this` argument. Thanks you both very much for answering.

Re: Struct copy constructor with inout

2023-11-14 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 13:41:32 UTC, Steven Schveighoffer wrote: ``` Error: copy constructor `testinoutctor.S1.this(ref const(S1) s) const` is not callable using argument types `(const(S1))` ``` I'm not sure what this means. There shouldn't be a copy being made here, as the thing is

Re: Struct copy constructor with inout

2023-11-14 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 08:50:34 UTC, dhs wrote: ```d struct S2 { this(ref inout S2 s) inout { writeln("copy"); } int i; } void test() { const(S1) s1; S1 ss1 = s1; // error, ss1 not qualified as const const(S2) s2; S2 ss2 = s2; // fine, why? } ``` Isn't "inout"

Re: Struct copy constructor with inout

2023-11-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 08:50:34 UTC, dhs wrote: I am using following code: ```d struct S1 { this(ref const S1 s) const { writeln("copy"); } int i; } struct S2 { this(ref inout S2 s) inout { writeln("copy"); } int i; } void test() { const(S1) s1; S1 ss1 = s1;

Re: Struct copy constructor with inout

2023-11-14 Thread dhs via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 09:07:24 UTC, Alexandru Ermicioi wrote: Seems like it isn't called at all, your copy constructor with inout. Could be a bug. My assumption is that default copy constructors are generated alongside inout one, and then picked up for your initialization instead

Re: Struct copy constructor with inout

2023-11-14 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 08:50:34 UTC, dhs wrote: In other words: why doesn't ss2=s2 fail here? Thanks, dhs Seems like it isn't called at all, your copy constructor with inout. Could be a bug. My assumption is that default copy constructors are generated alongside inout one, and

Struct copy constructor with inout

2023-11-14 Thread dhs via Digitalmars-d-learn
Hello D experts, I have a question regarding inout in struct copy constructors. From the spec: "The copy constructor can be overloaded with different qualifiers applied to the parameter (copying from a qualified source) or to the copy constructor itself (copying to a qualified destination)"