模板双缓冲区类
我有一个包装相同类型的2个缓冲区的课程,我需要一些用于这些缓冲区的固定器和获取器,我不想重复代码。我想用模板来实现这一目标,但我不知道该怎么做。我放了一些错误的代码只是为了理解这个想法:
class DualBuffer{
public:
template <int idx>
inline float<idx>* arr() {
// ...
}
private:
float* arr0;
float* arr1;
};
int main(){
DualBuffer db;
float* arr0 = db.arr<0>(); // returns a ptr to arr0
float* arr1 = db.arr<1>(); // returns a ptr to arr1
}
这是可以实现的吗?有人可以用一些实施细节指向正确的方向吗?
I've a class that wraps 2 buffers of the same type, I need some setters and getters for these buffers and I don't want to duplicate code. I'd like to achieve this with templates but I don't know how to do it. I put some wrong code just to get the idea:
class DualBuffer{
public:
template <int idx>
inline float<idx>* arr() {
// ...
}
private:
float* arr0;
float* arr1;
};
int main(){
DualBuffer db;
float* arr0 = db.arr<0>(); // returns a ptr to arr0
float* arr1 = db.arr<1>(); // returns a ptr to arr1
}
Is this achievable? Can someone point me in the right direction with some implementation details?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是使用
std ::数组
。但是,对于此特定示例,您还可以使用定义的方法arr
,而无需它作为函数模板,如下所示:The simplest way would be to use
std::array
. But for this particular example you can also use the methodarr
that you defined without needing it to be a function template as shown below: