Thread overview
[Issue 20695] Copy constructor disable default struct constructor
Mar 26
RazvanN
Mar 26
RazvanN
6 days ago
Mike Parker
6 days ago
Mathias LANG
6 days ago
Mathias LANG
6 days ago
RazvanN
March 26
https://issues.dlang.org/show_bug.cgi?id=20695

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #1 from RazvanN <razvan.nitu1305@gmail.com> ---
This is not related to copy constructors:

struct K
{
    int a;
    string b;
    this(int, int, int) {}
}

void main()
{
    K s = K(7, "gigi");
}

It seems that once you define a constructor, you are not able to use the default constructor.

--
March 26
https://issues.dlang.org/show_bug.cgi?id=20695

--- Comment #2 from RazvanN <razvan.nitu1305@gmail.com> ---
I can't find anything in the spec regarding this aspect.

--
6 days ago
https://issues.dlang.org/show_bug.cgi?id=20695

Mike Parker <aldacron@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |aldacron@gmail.com

--- Comment #3 from Mike Parker <aldacron@gmail.com> ---
It's always been the case that implementing opCall or a constructor disables
struct literals and initializers (except the default initializer T()), but it's
not currently in the spec (I'm sure it was at one time).

>From TDPL 7.1.3.1:

"The presence of at least one constructor disables all of the field-oriented constructors discussed above..."

And by "field-oriented constructors", he's referring to literals and initializers. The example:

```
struct Test {
    double a = 0.4;
    double b;
    this(double b) {
        this.b = b;
    }
}

auto t1 = Test(1.1, 1.2); // Error
    // No constructor matches Test(double, double)
static Test t2 = {0.0, 1.0}; // Error
    // No constructor matches Test(double, double);
```

The spec needs to be updated.

--
6 days ago
https://issues.dlang.org/show_bug.cgi?id=20695

--- Comment #4 from Mathias LANG <pro.mathias.lang@gmail.com> ---
> It's always been the case that implementing opCall or a constructor disables struct literals and initializers

`opCall` does not disable literals. And it technically *hides* constructors, I'm sure there are some convoluted way to show it.

> This is not related to copy constructors: [...]

I'm well aware of the disappearance of default ctors in the presence of a single definition. While I would like for us to have a way to control it, a la "this() = default" (from C++), copy ctors are not *regular* ctors, and I don't think they should disable the default constructors.

--
6 days ago
https://issues.dlang.org/show_bug.cgi?id=20695

Mathias LANG <pro.mathias.lang@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Hardware|x86                         |All
                 OS|Mac OS X                    |All
           Severity|enhancement                 |normal

--
6 days ago
https://issues.dlang.org/show_bug.cgi?id=20695

--- Comment #5 from RazvanN <razvan.nitu1305@gmail.com> ---
(In reply to Mathias LANG from comment #4)
> > It's always been the case that implementing opCall or a constructor disables struct literals and initializers
> 
> `opCall` does not disable literals. And it technically *hides* constructors, I'm sure there are some convoluted way to show it.
> 
> > This is not related to copy constructors: [...]
> 
> I'm well aware of the disappearance of default ctors in the presence of a single definition. While I would like for us to have a way to control it, a la "this() = default" (from C++), copy ctors are not *regular* ctors, and I don't think they should disable the default constructors.

Actually, copy constructors behave exactly as normal constructors, except that they may be called implicitly in some situations.

--