Skip to content

Should we have a way to perform C++ default initialization? #6739

@zygoloid

Description

@zygoloid

The testcase where I encountered this involves an explicit default constructor:

import Cpp inline '''
struct A {
    explicit A();
    A(const A&) = delete;
};
''';
var a: Cpp.A = ();

Currently we reject this, with a confusing error:

<source>:7:1: error: attempt to use a deleted function
    7 | var a: Cpp.A = ();
      | ^
<source>:4:5: note: 'A' has been explicitly marked deleted here
    4 |     A(const A&) = delete;
      |     ^

<source>:7:1: error: cannot implicitly convert expression of type `()` to `Cpp.A`
var a: Cpp.A = ();
^~~~~~~~~~~~
<source>:7:1: note: type `()` does not implement interface `Core.ImplicitAs(Cpp.A)`
var a: Cpp.A = ();
^~~~~~~~~~~~

The reason we reject is because C++ copy-initialization from {} is invalid because it would use an explicit constructor.

However, this leaves us with no easy way to provide initialization with the same semantics as C++'s

A a;

... which does permit calling an explicit default constructor. Should we have an ergonomic way to do that? Should the conversion from () do that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    examples-2026An issue that is relevant for examples we are trying to port to Carbon in 2026.leads questionA question for the leads team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions