Skip to content

clang rejects valid friend template function declaration with noexcept specifier in template class #101330

@Rush10233

Description

@Rush10233

The following code is rejected by clang for the mismatch of the noexcept specifier between the friend declaration and definition of func:

template <typename T>
struct C {
    template <int N>
    friend void func() noexcept(N == 0);
};

template <int N>
void func() noexcept(N == 0) {}

int main() {
    C<int> t;
    return 0;
}

The diagnostic says that:

<source>:4:17: error: exception specification in declaration does not match the previous declaration
    4 |     friend void func() noexcept(N == 0);
      |                 ^
<source>:11:12: note: in instantiation of template class 'C<int>' requested here
   11 |     C<int> t;
      |            ^
<source>:8:6: note: previous declaration is here
    8 | void func() noexcept(N == 0) {}

That is quite unreasonable. It should be clear that the noexcept expression of func keeps identical between the friend declaration and definition statement. Removing the dependent name N in the noexcept expression or replacing template class C with a non-template one can eliminate the diagnostic.

https://godbolt.org/z/rPh84Phzn

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"rejects-valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions