获取错误默认模板参数可能不会在函数模板中使用
Getting error default template arguments may not be used in function templates
我有一个带有默认模板参数的模板类。我在编译此代码时收到"函数模板中可能无法在函数模板中使用默认模板参数"错误,用于定义MyClass<T, n>::empty()
。我正在 gcc 版本 4.2.4(ubuntu 10)上编译代码:
template<typename T, int n=10>
class MyClass{
T Val[n];
int Capacity;
int Size;
public:
MyClass():Capacity(n), Size(0) {}
bool empty();
};
template<typename T, int n=10>
bool MyClass<T, n>::empty() {
return Size?false:true;
}
假设问题是如何使代码编译,则可以从empty()
实现的模板参数列表中删除默认值:
template<typename T, int n>
bool MyClass<T, n>::empty() {
return Size ? false : true;
}
请注意,需要在MyClass<T>
实例中调用empty()
的代码必须可以访问该实现。它不能在实现文件中编译。
相关文章:
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 使用具有默认参数的函数模板进行 decltype 会使结果混乱(一个有趣的问题或 gcc 的错误)
- 从模板函数返回默认构造或无效
- 为模板类中的纯虚函数提供默认实现
- 具有专用成员函数的默认模板参数
- C++模板函数的默认参数的 ODR 规则
- 是否可以使用 T::运算符< 作为模板函数的默认参数?
- 尝试将 C++11 代码转换为 C++03 时默认函数模板参数出错
- 在集合上运行的函数模板,该集合具有默认的"transform"函数,该函数不执行任何操作
- CUDA9.2及以上版本中模板默认参数存在无法解决的外部函数错误
- MSVC 编译器实例化函数模板的默认定义,即使存在专用化
- 已忽略函数的默认模板参数
- 不存在的标识符在非启用函数模板中用作默认参数
- 调用基默认构造函数模板类
- 获取错误默认模板参数可能不会在函数模板中使用
- 使用静态模板函数的默认参数编译错误
- 具有默认模板参数的友元函数模板
- 将模板函数与默认参数混合时出错
- 是否可以使函数模板对不适当的类型而不是错误执行默认操作
- 函数模板:默认类型参数