Template friend declaration of a member of a dependent type

Viewed 73

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?

0 Answers
Related