我正在阅读“Stroustrup 的 C++ 编程语言”,其中提到了
Note that the template parameter lists of a template and its template member cannot be combined.
示例如下。
template<class T>
template<class T2> Ptr<T>::operator Ptr<T2> () { return Ptr<T2>(p); }
template<class T, class T2> // error
Ptr<T>::operator Ptr<T2> () { return Ptr<T2>(p); }
我不明白为什么不允许这样做,有人可以解释为什么不允许吗?
最佳答案
因为这是两个不同的模板(模板化类和该类的模板化成员函数),并且无论谁起草规范,都决定有必要使其在函数定义中保持可见。
我认为这是完全合乎逻辑的。如果按照您建议的方式完成,则阅读您代码的程序员可能会认为有一个带有两个参数的模板类,那么他就错了。
关于c++ - 为什么模板及其模板成员的模板参数列表不能合并?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52142778/