通过看下面的代码,我对第3行感到困惑。
第3行不是基础模板的特殊情况,它更像是“类重载”。但是可以成功编译。
第7行中的obj1是根据第3行定义的,但编译失败。
怎么来的?
template<typename S,int T, void(* U)()> class Bar{}; // Base template
template<int T, void(* U)()> class Bar<double, T, U>{}; // Specialization, which is good
template<int T, void(* U)()> class Bar<double, U, T>{}; // Also good, how come?
void func(){};
int main(){
//Bar<double, func, 1> obj1; // Error, from line 3
}
最佳答案
只要在任何地方都不使用该表格,编译器就不会抱怨。如果使用它,并且编译器必须实例化-那么它将抱怨。这就是为什么如果您不加注释,则会得到错误-在这一点上,编译器会看到有缺陷的部分特化。