【发布时间】:2014-05-07 19:13:48
【问题描述】:
我正在编写一个矢量类,我希望它具有以下特点:
- 尽可能在堆栈上使用静态分配(以避免调用 new 以提高效率)。
- 如果用户更愿意提供以前分配的数组,则可以从指针实例化。
- 类需要轻松转换为简单指针。这允许使用以前在 C 中编写的例程。
在这个简单的测试问题下面找到我想出的解决方案。我使用继承,所以 Vector 从 Vector_base 继承,它为所有向量提供了一个公共接口(纯虚拟)。 然后我定义了一个空类 Vector ,它允许我使用部分特化来拥有不同的存储方案;静态或动态。
这背后的想法是,我只想让 vector 成为老式静态数组的 C++ 包装器。
我喜欢下面的实现。我想将我想出的界面保留在 main.js 中。
我不喜欢 sizeof(Vector3) = 32 在 C 中,三个双精度的向量是 24 字节。原因是虚拟表多出了 8 个字节。
我的问题:我能否想出另一种设计,为我提供相同的接口,但向量只有 24 个字节?
总结:
- 我想要一个 24 字节的 Vector3,就像在 C 中一样。
- 我仍然想要任意大的向量(使用
<double,n>) - 我想保留 main() 中使用的接口。
我可以为此使用诸如特征或策略之类的编程习惯吗?我对这些很陌生,我不知道他们是否可以提供解决方案。
在下面找到我的小测试代码:
#include <iostream>
using namespace std;
#define TRACE0(a) cout << #a << endl; a;
#define TRACE1(a) cout << #a "=[" << a << "]" << endl;
enum alloc_type {Static,Dynamic};
template <class T>
class Vector_base{
public:
Vector_base(){}
virtual operator T*() = 0;
virtual T operator[](int i)const = 0;
virtual T& operator[](int i) = 0;
virtual int size() const = 0;
friend ostream& operator<<(ostream &os,const Vector_base& v){
for (int i=0; i<v.size(); i++)
cout << v[i] << endl;
return os;
}
};
// base template must be defined first
template <class T, int n,alloc_type flg=Static>
class Vector{};
//Specialization for static memory allocation.
template <class T, int n>
class Vector<T,n,Static>: public Vector_base<T>{
public:
T a[n];
public:
Vector() {
for (int i=0; i<n; i++) a[i] = 0;
}
int size()const{return n;}
operator T*(){return a;}
T operator[](int i)const {return a[i];}
T& operator[](int i){return a[i];}
};
//Specialization for dynamic memory allocation
template <class T,int n>
class Vector<T,n,Dynamic>: public Vector_base<T>{ //change for enum. flg=0 for static. flg=1 for dynamic. Static by default
public:
T* a;
public:
Vector():a(NULL){
}
Vector(T* data){ //uses data as its storage
a = data;
}
int size()const{return n;}
operator T*(){return a;}
T operator[](int i)const {return a[i];}
T& operator[](int i){return a[i];}
};
//C++11 typedefs to create more specialized three-dimensional vectors.
#if (__cplusplus>=201103L)
template <typename Scalar,alloc_type flg=Static>
using Vector3 = Vector<Scalar,3,flg>;
#else
#error A compiler with the C++2011 standard is required!
#endif
int main(){
cout << "Testing Vector3: " << endl;
//Vector<double,3> v3;
Vector3<double> v3;
TRACE0(cout << v3 << endl;);
TRACE1(sizeof(v3));
//Vector<double,3,Dynamic> v0(v3);
Vector3<double,Dynamic> v0(v3); //calls Vector<double,3,Dynamic>::Vector(double*) and uses the conversion operator on v3.
TRACE1(sizeof(v0));
TRACE1(sizeof(double*));
TRACE0(v3[1] = 2.1;);
TRACE0(cout << v0 << endl;);
return 0;
}
【问题讨论】:
-
std::vector有什么问题? -
@Jefffrey,这是针对数值计算的非常有效的实现。这个想法是调用非常专门的 C 例程来执行点/叉积、范数、矩阵向量乘法等。std::vector 不是为此目的而设计的。
-
@BenVoigt 使用
std::array? v0v -
@BenVoigt,然后使用
std::vector。你能看到一个模式吗? -
@DeadMG:OP 的接口是多态的,所以你的算法可以无缝地处理任何一种向量。
标签: c++ templates c++11 traits partial-specialization