Wal*_*ter 5 c++ gcc templates compiler-errors function-declaration
我对gcc 5.1.0有一个奇怪的问题.以下最小代码
// 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;
} }
Run Code Online (Sandbox Code Playgroud)
导致编译器错误
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;
Run Code Online (Sandbox Code Playgroud)
如果删除源中指示的行,则错误消失.代码用clang ++编译好,但gcc 4.8也显示错误.我试图找到关于gcc bugzilla的相关错误报告但没有成功,但这可能没有任何意义.所以我的主要问题是:这确实是一个gcc bug,如果是这样,它是新的吗?