4

In the section "13.7.4 Friends" of the C++ 20 Standard there is an example of declaring a template friend declaration of a member of a dependent type.

I have simplified the example leaving the declaration that arises a question.

Here you are.

#include <iostream>

template <class T>
struct A
{
    T h();
};

template <>
struct A<float *>
{
    int * h();
};

class C
{
    template <class T>
    friend int *A<T *>::h();  // grants friendship to A<int*>::h() and A<float*>::h()
};

int main()
{
}

But neither MS Visual Studio 2019, nor for example gcc 8.3 compiles the code.

For example the last mentioned compiler issues message

prog.cpp:21:24: error: member ‘int* A<T*>::h()’ declared as friend before type ‘A<T*>’ defined
  friend int *A<T *>::h();  // grants friendship to A<int*>::h() and A<float*>::h()
                        ^

Is it a bug of the compilers or have I missed something?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • fyi - live using trunk (or latest) clang:yes(warning); gcc:no; MSVC:yes https://godbolt.org/z/EzoWEfYPn – Richard Critten Apr 25 '21 at 23:00
  • Using [temp.friend-example-4](https://eel.is/c++draft/temp.decls#temp.friend-example-4), all compilers are wrong [Demo](https://godbolt.org/z/d4G9h11M3): clang/gcc won't accept valid cases, msvc accept the wrong case. – Jarod42 Apr 26 '21 at 13:43

0 Answers0