[Issue 20876] generated constructor always inout regardless of ability of fields to use inout

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Oct 10 23:20:02 UTC 2024


https://issues.dlang.org/show_bug.cgi?id=20876

Paul Backus <snarwin+bugzilla at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |snarwin+bugzilla at gmail.com

--- Comment #17 from Paul Backus <snarwin+bugzilla at gmail.com> ---
(In reply to Walter Bright from comment #16)
> It seems like a simple solution would be:
> 
> 1. if all fields take a const argument, then generate a const copy
> constructor
> 
> 2. if all fields take an immutable argument, then generate an immutable copy
> constructor
> 
> 3. otherwise, generate a mutable copy constructor

The problem with this approach is that it is not always possible to find a
single copy constructor signature that's compatible with all field copy
constructors.

For example:

---
struct S
{
    this(const ref S) {}
    this(shared ref S) {}
}

struct T
{
    S s;
    // If all fields take a const argument, then
    // generate a const copy constructor.
    this(const ref T other)
    {
        this.s = other.s;
    }
}

void main()
{
    {
        shared S original;
        S copy = original; // ok
    }
    {
        shared T original;
        T copy = original; // fails
    }
}
---

In this case, T needs to have two copy constructor overloads (a const one and a
shared one) in order to correctly preserve the behavior of S.

--


More information about the Digitalmars-d-bugs mailing list