I have a strange problem with gcc 5.1.0. The following minimal code
// header 1
namespace A {
template<typename X>
inline constexpr X square(X x) { return x*x; }
}
// header 2
namespace A { namespace B {
template<typename X>
struct matrix { X A[3][3]; };
template<typename X>
matrix<X> square(matrix<X> const&) noexcept;
} }
// source, includes both headers
namespace A { namespace B {
using A::square; // no problem without this line
template<typename X>
matrix<X> square(matrix<X> const&M) noexcept
{
matrix<X> R;
for(int i=0; i!=3; ++i)
for(int j=0; j!=3; ++j)
R.A[i][j] = M.A[i][0]*M.A[0][j] +
M.A[i][1]*M.A[1][j] +
M.A[i][2]*M.A[2][j] ;
return R;
}
template matrix<double> square(matrix<double> const&) noexcept;
} }
causes the compiler error
test.cc:28:59: error: ‘A::B::matrix<double> A::B::square(const A::B::matrix<double>&)’ is not declared in ‘A::B’
template matrix<double> square(matrix<double> const&) noexcept;
The error disappears if the line indicated in the source is removed. The code compiles fine with clang++, but gcc 4.8 also shows the error. I tried without success to find a related bug report on gcc bugzilla, but that may not mean anything. So my main question(s): is this indeed a gcc bug and, if so, is it new?
This may well be related to the ancient bug 37374, which showed similar behaviour without template. I have filed a new bug report.