我有以下代码:
class D{/* */};
class P{/* */};
class U{/* */};
// Generic version not defined
template <typename R>
class UEB;
// Specialization
template <>
class UEB<D> {
public:
explicit UEB(D& /* r */)
{}
};
// Specialization
template <>
class UEB<P> {
public:
explicit UEB(P& /* r */)
{}
};
template <>
class UEB<U> {
public:
explicit UEB(U& /* r */)
{}
};
int main()
{
D d;
P p;
U u;
UEB<D> ud(d);
UEB<P> up(p);
UEB<U> uu(u);
return 0;
}
使用GCC 7,8和-Wall -pedantic -Wextra
进行编译。
然而,cppcheck抱怨UEB<P>
和UEB<U>
没有构造函数。
所以我将explicit UEB(U& /* r */)
改为explicit UEB<U>(U& /* r */)
(和P类似),不仅仅是编译,cppcheck停止抱怨。
我的问题是:为什么这有效?在声明显式专用模板类的构造函数时,模板参数是否可选?