D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20695 - Copy constructor disable default struct constructor
Summary: Copy constructor disable default struct constructor
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: C++, pull, rejects-valid
Depends on:
Blocks:
 
Reported: 2020-03-23 08:53 UTC by Mathias LANG
Modified: 2021-01-17 11:33 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Mathias LANG 2020-03-23 08:53:52 UTC
```
struct Bar
{
    this(const ref Bar o) {}

    string a;
    uint b;
}

void main ()
{
    // foo.d(11): Error: struct Bar has constructors, cannot use { initializers }, use Bar( initializers ) instead
    Bar b = { a: "Hello", b: 42 };
    // foo.d(12): Error: copy constructor foo.Bar.this(ref const(Bar) o) is not callable using argument types (string\
, int)
    // foo.d(12):        cannot pass rvalue argument "Hello" of type string to parameter ref const(Bar) o
    Bar c = Bar("Hello", 42);
}
```

Expected result: This should compile. It should also work if the constructor is `@disable`d.
Comment 1 RazvanN 2020-03-26 02:32:32 UTC
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.
Comment 2 RazvanN 2020-03-26 02:39:47 UTC
I can't find anything in the spec regarding this aspect.
Comment 3 Mike Parker 2020-03-26 08:03:27 UTC
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.
Comment 4 Mathias LANG 2020-03-26 08:32:56 UTC
> 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.
Comment 5 RazvanN 2020-03-26 08:39:17 UTC
(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.
Comment 6 Dlang Bot 2021-01-14 10:07:44 UTC
@RazvanN7 created dlang/dmd pull request #12132 "Fix Issues 20695, 21547 - CpCtor disables default construction + Struct initializers are disabled wrongfully" fixing this issue:

- Fix Issues 20695, 21547 - CpCtor disables default construction + Struct initializers are disabled wrongfully

https://github.com/dlang/dmd/pull/12132
Comment 7 Dlang Bot 2021-01-17 11:33:11 UTC
dlang/dmd pull request #12132 "Fix Issues 20695, 21547 - CpCtor disables default construction + Struct initializers are disabled wrongfully" was merged into master:

- b61df05dd3d5d9792452c496d07fe04f2df12993 by RazvanN7:
  Fix Issues 20695, 21547 - CpCtor disables default construction + Struct initializers are disabled wrongfully

https://github.com/dlang/dmd/pull/12132