2

I have a piece of code which works with g++/clang++. It has recently been reported to me that it breaks with Visual C++.

The code is this:

namespace q {
    template <typename X, typename Y>
    struct A {};
}

template <typename X>
struct B {
    template <typename Y>
    friend struct q::A;
};

int main() {
    return 0;
}

VC++ returns the following error:

source_file.cpp(9): error C2976: 'q::A': too few template arguments
source_file.cpp(3): note: see declaration of 'q::A'
source_file.cpp(10): note: see reference to class template instantiation 'B<X>' being compiled

Who is correct? Is there a portable way to do this?

Svalorzen
  • 5,353
  • 3
  • 30
  • 54

1 Answers1

1

Writing template parameters properly should help:

template <typename X, typename Y>
friend struct q::A;

Note that incorrectly declaring A as a friend makes program ill-formed, no diagnostic is required.

user7860670
  • 35,849
  • 4
  • 58
  • 84
  • What I had hoped to achieve was to declare as friend only `q::A` where the `X` template parameter is the same as the enclosing `B` class. I now realize that what I wrote may not do what I intended, and that what I want may be a partial specialization, which is forbidden. Am I correct, or is there a way to do this? – Svalorzen Sep 22 '18 at 12:51
  • 1
    @Svalorzen [](https://stackoverflow.com/questions/27750755/partial-specialization-friend-declaration) – user7860670 Sep 22 '18 at 13:09