类封装的模板参数的默认参数 [英] default argument for template parameter for class enclosing
问题描述
代码:
template <typename element_type, typename container_type = std::deque<element_type> >
class stack
{
public:
stack() {}
template <typename CT>
stack(CT temp) : container(temp.begin(), temp.end()) {}
bool empty();
private:
container_type container;
};
template <typename element_type, typename container_type = std::deque<element_type> >
bool stack<element_type, container_type>::empty()
{
return container.empty();
}
编译时会报错。
默认参数为包含
'bool stack< element_type,container_type> :: empty()'
/ p>
default argument for template parameter for class enclosing
'bool stack<element_type,container_type>::empty()'
为什么编译器抱怨,如何使它工作?
Why is the compiler complaining and how can I make it work?
推荐答案
您尝试为 stack
提供第二个模板参数的默认参数两次。默认模板参数,就像默认函数参数一样,只能定义一次(每个转换单位);甚至不允许重复完全相同的定义。
You attempt to provide a default argument for the second template parameter to stack
twice. Default template arguments, just like default function arguments, may only be defined once (per translation unit); not even repeating the exact same definition is allowed.
只需在您定义类模板的开头键入默认参数。之后,舍弃:
Just type the default argument at the beginning where you define the class template. After that, leave it out:
template<typename element_type,typename container_type>
bool stack<element_type,container_type>::empty(){
return container.empty();
}
这篇关于类封装的模板参数的默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!