The following minimal example put in a .cpp file causes a compiler error in VisualStudio 2017 (15.7.4, with /std:c++17 enabled):
template <typename T>
class A
{
void f(T& t)
{
t.g<2>();
}
};
// B is a candidate for the template parameter T in A
struct B {
template <int a>
void g() {
// nothing here
}
};
The compiler error refers to the body of A::f() and reads:
error C2760: syntax error: unexpected token ')', expected 'expression' .
Note that neither A nor B are actually instantiated. The same error also occurs without declaring B.
However, if I switch the order in which A and B are declared, the example compiles (and A can be instantiated). This is surprising, because I have not expressed any relation between A and B in the code. Another surprising fact is that adding an argument (e.g. an int) to the signature of B::g() and the call of g() also seems to solve the error (without changing the order of declaration).
My questions are: (1) Why does the code above produce a compiler error? (2) How I can resolve it in a clean way?