带有非数据类型模板参数的类模板特化
class template specialization with nondatatype template parameter
我一直在尝试以下代码:
#include <iostream>
using namespace std;
template <typename T, int SIZE>
class Stack
{
T element;
T size[SIZE];
public:
void setElement (T elmt)
{
element = elmt;
cout <<"Inside setElement"<<endl;
};
Stack()
{
cout <<"Constructor "<<endl;
cout <<" SIZE is "<<SIZE<<endl;
}
};
// Int class Specialization.
template<>
class Stack<int>
{ int element;
int size[SIZE];
public:
void setElement (int elmt)
{
element = elmt;
cout <<"Inside setElement"<<endl;
};
Stack()
{
cout <<"Constructor "<<endl;
cout <<" SIZE is "<<SIZE<<endl;
}
}
int main(int argc, char ** argv)
{
Stack<float,50> s;
Stack<int,20> s1;
}
我有专门的Stack模板类为'int'数据类型。
但我不知道如何将非类型模板参数'int SIZE'在模板内,而特殊化模板类?
我得到以下编译错误:
temp1.cpp:26:7: error: too few template arguments for class template 'Stack'
class Stack<int>
^
temp1.cpp:5:7: note: template is declared here
class Stack
^
temp1.cpp:44:1: error: cannot combine with previous '(error)' declaration specifier
int main(int argc, char ** argv)
^
temp1.cpp:44:5: error: no function template matches function template specialization 'main'
int main(int argc, char ** argv)
如何做到这一点?
您的专门化中缺少SIZE
模板参数。
模板声明应该是:
template<int SIZE>
class Stack<int, SIZE>
相关文章:
- 通过依赖类型使用非类型模板参数的单类型模板参数类模板的部分专用化
- GCC/CLang不同意模板模板参数的部分特化
- 类模板专用化演绎是否应该考虑演绎指南参数初始化?
- 使用每种类型的可变参数模板上的类模板初始化元组
- 具有模板模板参数的类模板专用化
- 初始化和访问可变参数类模板的成员
- 显式使用类模板实例化中某些参数的默认值
- 类模板专用化中模板参数的默认值
- 在类模板特化中使用sizeof模板参数包
- 带有模板化参数的部分特化函数模板
- 模板类的特化成员-不匹配-数组
- 可以创建匹配枚举类型的类模板的部分模板特化吗?
- 带有非数据类型模板参数的类模板特化
- 带有抽象基类的子类的模板特化
- 模板特化中参数包的大小
- 类模板特化,c++,模板形参列表中参数2不匹配
- 组类模板特化
- 模板特化中对基类成员的未定义引用
- 解析模板化类的特化静态成员变量的定义
- 作为模板函数参数的完全特化类