具有可变模板的自定义容器模板
Custom Container emplace with variadic templates
本文关键字:自定义 更新时间:2023-10-16
我正在实现一个简单的循环向量类。我想实现一个模板成员函数,但我遇到了一个我不理解的错误。这可能是对我做错的事情的简单修复,但由于我对可变模板没有太多经验,我不知道是什么。。。
我得到的错误是:
main.cpp: In function 'int main()':
main.cpp:104:50: error: no matching function for call to 'CircularVector<Item>::emplace(int, int, int, int, int, int, std::vector<int>, int)'
v.emplace(1, 0, 1, 0, 1, 0, vector<int>(), -1);
^
main.cpp:104:50: note: candidate is:
main.cpp:20:7: note: void CircularVector<T, Args>::emplace(const Args& ...) [with T = Item; Args = {}]
void emplace(const Args &... args) {
^
main.cpp:20:7: note: candidate expects 0 arguments, 8 provided
生成此错误的源代码位于http://coliru.stacked-crooked.com/a/37d50d6f23363357):
#include <vector>
using namespace std;
#define CIRCULAR_BUFFER_DEFAULT_SIZE 5000
template <typename T, typename ...Args>
class CircularVector {
public:
CircularVector(int size) {
_size = size;
_v.reserve(_size);
}
CircularVector() {
_size = CIRCULAR_BUFFER_DEFAULT_SIZE;
_v.reserve(_size);
}
void emplace(const Args &... args) {
++Count;
++_indexWrite;
if (_indexWrite > _size - 1) _indexWrite = 0;
_v.emplace(_indexWrite, args...);
}
void push(const T& item) {
++Count;
++_indexWrite;
if (_indexWrite > _size - 1) _indexWrite = 0;
_v[_indexWrite] = item;
}
void pop(T& item) {
item = _v[_indexRead];
++_indexRead;
if (_indexRead > _size - 1) _indexRead = 0;
--Count;
}
T& back() {
return _v[(_indexRead + Count - 1) % _size];
}
void erase(int numItems) {
_indexRead += numItems;
if (_indexRead > _size - 1) _indexRead -= _size;
Count -= numItems;
}
void eraseAt(int index) {
swap(_v[index], _v[(_indexRead + Count - 1) % _size]);
--Count;
--_indexWrite;
if (_indexWrite < 0) {
_indexWrite = _size - 1;
}
}
void clear() {
_indexRead = 0;
_indexWrite = -1;
Count = 0;
}
T& operator[](std::size_t idx) {
int index = _indexRead + idx;
if (index > _size) index = index % _size;
return _v[index];
};
int Count = 0;
private:
int _indexWrite = -1;
int _indexRead = 0;
int _size = 0;
std::vector<T> _v;
};
class Item {
public:
double A;
int B;
int C;
vector<int> D;
int E;
Item(double a, int b, int c, vector<int> &d, int e) {
A = a;
B = b;
C = c;
D = d;
E = e;
}
};
int main() {
CircularVector<Item> v;
v.emplace(1, 0, 1, 0, 1, 0, vector<int>(), -1);
}
如果有人遇到同样的问题,我就是这样做到的:
void emplace(Args&&... args) {
++Count;
++_indexWrite;
if (_indexWrite > _size - 1) _indexWrite = 0;
_v.emplace(_v.begin() + _indexWrite, std::forward<Args>(args)...);
}
尽管我真正想要的是使用该索引中的保留内存来构造一个元素,而不是在那个特定位置插入一个新元素。
Args
必须是emplace
的模板参数,而不是CircularVector
。
template <typename T>
class CircularVector {
public:
/* ... */
template<typename ...Args>
void emplace(const Args &... args) {
/* ... */
}
};
此外,您应该考虑对emplace
使用转发引用。
相关文章:
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++自定义比较函数
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 如何正确实现和访问运算符的各种自定义枚举器
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 自定义先决条件对移动分配运算符有效吗
- 使用VS Code和CMake Tools运行自定义命令
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- std::ranges::elements_view,用于自定义类似元组的数据
- 跟随整数索引列表的自定义类迭代器
- 参数化自定义CMake工具链
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 如何在自定义类中启用'auto loops'?
- 使用QJsEngine在Qt中注册自定义类型
- Qt自定义QPush按钮未显示在布局上
- 自定义对象的dlib序列化在gcc中失败
- 自定义创建QFuture
- 如何在QT中的自定义视频小工具t上绘制矩形