Skip to content

Clang doesn't recognise friendship in default parameters in C++20+ #83608

Open
@cjdb

Description

@cjdb

This code doesn't compile on Compiler Explorer. Moving the defaulted parameter to the constructor body seems to be okay.

template<class T>
class single;

template<class T, class U>
class check_constructible {
    check_constructible() = default;

    static_assert(is_constructible_v<T, U>);

    template<class X>
    friend class single;
};

template<class T>
struct single {
    template<class U>
    single(U u, check_constructible<T, U> = {})
    : x(u)
    {}

    T x;
};

int main() {
    single<int> x(0);
}

Diagnostic:

<source>:25:45: error: calling a private constructor of class 'check_constructible<int, int>'
   25 |     single(U u, check_constructible<T, U> = {})
      |                                             ^
<source>:14:5: note: implicitly declared private here
   14 |     check_constructible() = default;
      |     ^

Compiler info:

clang version 19.0.0git (https://github.com/llvm/llvm-project.git 5b058709536dd883980722ee000bb7b8c7b2cd8b)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/compiler-explorer/clang-trunk-20240301/bin
Compiler returned: 0

Both GCC and MSVC accept this code. If it's not conforming, should we accept it as an extension?

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second partyrejects-valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions