Skip to content

std::variant of inner class cannot be created if inner class has initializers #124088

Open
@tbleher

Description

@tbleher

The following code does not build:

#include <variant>

struct X {
    struct A { int a=0; };
    struct B {
        std::variant<X::A> a;
    };
};

void test() {
    X::B b;
}

See https://godbolt.org/z/4ExGEvTf6 (on clang-trunk, with -std=c++20 -O2).

The error is:

<source>:11:10: error: call to implicitly-deleted default constructor of 'X::B'
   11 |     X::B b;
      |          ^
<source>:6:28: note: default constructor of 'B' is implicitly deleted because field 'a' has no default constructor
    6 |         std::variant<X::A> a;
      |                            ^

Each of the following changes will make the code compile:

  • making A and B top-level classes
  • Changing class A to not have an initializer for a
  • Changing class A to have an explicit constructor (like A() : a(0) {})

I think this code should be accepted. GCC accepts this code since version 14.1 (previous versions reject it with an error message similar to clang's message).

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"conceptsC++20 conceptscwg-issueAn issue that was filed to the Core Working Grouplibstdc++GNU libstdc++ C++ standard library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions