将模板函数与默认参数混合时出错
Error in mixing template function with default argument
我有一个模板化类,我遇到了一些问题。我在另一个类中有这种入侵:
value.push_back(x);
作为 x unsigned int
,值称为 List<unsigned int>
的模板化类,并push_back此函数:
template <class T>
void List<T>::push_back(T a=T(),int l=1){
(*this).resize((*this).size+l,a);
}
我在代码块中有以下错误:
...mp.h|86|error: no matching function for call to 'List<unsigned int>::push_back(unsigned int)'
...mp.h|86|note: candidate is:
...list.h|36|note: void List<T>::push_back(T, int) [with T = unsigned int]
...list.h|36|note: candidate expects 2 arguments, 1 provided
我不知道该怎么做,该函数已经有 int 的默认值,并且我已经尝试了 2 个不同的编译器,我真的不想在push_back
中添加另一个参数以使其变得push_back(x,1)
.
是否在声明中包含默认值?
template <class T>
struct List
{
void push_back(T a=T(),int l=1);
};
一个好的编译器应该拒绝编译它,如果你没有(或者至少警告过这种差异),但是,只是为了确定。
习惯上"只是"在类中实现模板成员:
template <class T>
struct List
{
void push_back(T a=T(),int l=1)
{
(*this).resize((*this).size+l,a);
}
};
相关文章:
- 处理一般情况混合类型和非类型的可变参数模板
- 将 lambda 函数转换为具有混合 lambda 引入器和参数列表的函子结构
- 好奇的混合与可变参数构造函数
- 参数的混合值,当我调用指针成员函数时
- 混合 c++ 和汇编不能将多个参数从C++函数传递到程序集
- 有没有一种干净(更)的方法将 CRTP 与可变参数继承混合在一起?
- 混合模板类型的模板参数包(std::使用可选参数重新创建函数)
- 模板模板参数,具有混合类型和非类型variadic参数
- 在C++中混合模板参数和变量时的分支
- 在构造函数中混合可变参数模板和初始值设定项列表
- 混合可变参数模板值和可变参数推导类型
- 将构造函数与固定参数混合,将构造函数与构造函数模板混合在一起
- C++和混合编程中指针的默认参数
- 混合模板化参数类型
- 混合按引用传递和按值传递到可变参数模板函数有效
- 将模板函数与默认参数混合时出错
- 混合类型,可变长度参数列表(varargin,*args,..)用于C++
- 混合了部分模板专门化和默认模板参数
- 在c++ /Qt中拆分Lua函数调用的混合字符串/数字参数列表
- 在c++类中混合指向构造结构的指针——在参数列表中声明结构