Ale*_*dro 1 c++ templates partial-specialization traits c++11
我正在编写一个矢量类,我希望它具有以下特征:
通过我提出的解决方案在下面找到这个简单的测试问题.我使用继承,因此Vector继承自Vector_base,它为所有向量提供了一个通用接口(纯虚拟).然后我定义了一个空类Vector,允许我使用部分特化来拥有不同的存储方案; 静态或动态.
这背后的想法是我只想让vector成为旧式静态数组的C++包装器.
我喜欢下面的实现.我想保留我想出的主界面.
我不喜欢的是sizeof(Vector3)= 32,当在C中时,三个双精度的矢量是24个字节.原因是虚拟表的额外8个字节.
我的问题:我可以以某种方式提出另一种设计,它将为我提供相同的接口,但矢量只有24个字节?
总结:
<double,n>)我可以使用像特征或政策这样的编程习语吗?我是新手,我不知道他们是否能提供解决方案.
找到我的小测试代码如下:
#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;
}
Run Code Online (Sandbox Code Playgroud)
您想要的所有功能都作为标准提供,或者可以插入现有的标准扩展点.
尽可能在堆栈上使用静态分配(以避免为了提高效率而调用new).
相约std::array<T, N>.它是C数组上的C++包装器,具有所有相同的特性.
如果用户更喜欢提供先前分配的数组,则能够从指针实例化.
认识分配者.您可以编写满足已经分配内存的需求的分配器,然后简单地使用std::vector.这样的分配器正在考虑未来的标准以及其他分配器增强,如多态分配器.
该类需要轻松转换为简单的指针.这允许在C中使用以前编写的例程.
双方std::vector并std::array提供了这是一个平凡.
如果您想在运行时提供此选择,请考虑boost::variant.滚动你自己的歧视联盟 - 不建议.
| 归档时间: |
|
| 查看次数: |
319 次 |
| 最近记录: |