Skip to content

Cannot declare friend function template with unqualified name in C++20 mode without prior forward declaration #147177

Open
@Fedr

Description

@Fedr

This program

template <typename T>
class A {
    friend void f<>(A);
};

template <typename T>
void f(A<T>) {}

int main() {
    f(A<int>{});
}

is accepted in GCC and MSVC starting from C++20. But Clang keeps complaining:

error: no candidate function template was found for dependent friend function template specialization
    3 |     friend void f<>(A);
      |                 ^

Online demo: https://gcc.godbolt.org/z/vKjhY9WMv

It looks related to ADL changes for function templates lookup with explicitly-specified template arguments in C++20: https://en.cppreference.com/w/cpp/language/adl.html#Notes
GCC and MSVC both allow unqualified names in template friend declaration friend void f<>(A&);, which is accepted if function template f is declared after class A body but before its instantiation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"diverges-from:gccDoes the clang frontend diverge from gcc on this issuediverges-from:msvcDoes the clang frontend diverge from msvc on this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions