C++ 带有继承的部分专门化。我可以避免继承吗?
我正在编写一个向量类,我希望它具有以下特征:C++ 带有继承的部分专门化。我可以避免继承吗?,c++,templates,c++11,traits,partial-specialization,C++,Templates,C++11,Traits,Partial Specialization,我正在编写一个向量类,我希望它具有以下特征: 尽可能在堆栈上使用静态分配(避免调用new以提高效率) 如果用户希望提供先前分配的数组,则可以从指针实例化 该类需要轻松地转换为简单指针。这允许使用以前用C编写的例程 下面是这个简单的测试问题和我提出的解决方案。我使用继承,所以Vector继承自Vector_base,它为所有向量提供了一个公共接口(纯虚拟)。 然后我定义了一个空的类向量,它允许我使用部分专门化来拥有不同的存储方案;静态的或动态的 这个想法是,我只希望向量是一个C++包装器到老式静态
)#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;
#定义TRACE0(a)cout您可以将向量模板专门化简化为
template <class T, std::size_t Size = -1>
class Vector {
// The statically allocated implementation
};
template <class T>
class Vector<T, -1> {
// The dynamically allocated implementation
};
像这样实例化
Structure<int[]> heap(3);
Structure<int[3]> stack;
结构堆(3);
结构堆栈;
编辑:或像这样使用策略
class AllocationPolicy {
protected:
static const std::size_t Size = 0;
};
template<std::size_t Size_>
class Static : AllocationPolicy {
protected:
static const std::size_t Size = Size_;
};
class Dynamic : AllocationPolicy {
protected:
static const std::size_t Size = 0;
};
template <typename T, typename TAllocationPolicy = Dynamic>
class Vector : TAllocationPolicy {
static_assert(!std::is_same<typename std::remove_cv<TAllocationPolicy>::type, AllocationPolicy>::value && std::is_base_of<AllocationPolicy, TAllocationPolicy>::value, "TAllocationPolicy must inherit from AllocationPolicy");
using TAllocationPolicy::Size;
public:
T data[Size];
};
template <typename T>
class Vector<T, Dynamic> : private Dynamic {
T * data;
public:
Vector(std::size_t size) : data(new T[size]) {}
~Vector() { delete [] data; }
};
类分配策略{
受保护的:
静态常数std::size\u t size=0;
};
样板
类静态:分配策略{
受保护的:
静态常数std::size\u t size=size;
};
类动态:分配策略{
受保护的:
静态常数std::size\u t size=0;
};
样板
类向量:TAllocationPolicy{
静态断言(!std::is_same::value&&std::is_base_of::value,“TAllocationPolicy必须继承自AllocationPolicy”);
使用TAllocationPolicy::Size;
公众:
T数据[大小];
};
样板
类向量:私有动态{
T*数据;
公众:
向量(std::size\u t size):数据(新的t[size]){}
~Vector(){delete[]data;}
};
您可以将向量
模板专门化简化为
template <class T, std::size_t Size = -1>
class Vector {
// The statically allocated implementation
};
template <class T>
class Vector<T, -1> {
// The dynamically allocated implementation
};
像这样实例化
Structure<int[]> heap(3);
Structure<int[3]> stack;
结构堆(3);
结构堆栈;
编辑:或像这样使用策略
class AllocationPolicy {
protected:
static const std::size_t Size = 0;
};
template<std::size_t Size_>
class Static : AllocationPolicy {
protected:
static const std::size_t Size = Size_;
};
class Dynamic : AllocationPolicy {
protected:
static const std::size_t Size = 0;
};
template <typename T, typename TAllocationPolicy = Dynamic>
class Vector : TAllocationPolicy {
static_assert(!std::is_same<typename std::remove_cv<TAllocationPolicy>::type, AllocationPolicy>::value && std::is_base_of<AllocationPolicy, TAllocationPolicy>::value, "TAllocationPolicy must inherit from AllocationPolicy");
using TAllocationPolicy::Size;
public:
T data[Size];
};
template <typename T>
class Vector<T, Dynamic> : private Dynamic {
T * data;
public:
Vector(std::size_t size) : data(new T[size]) {}
~Vector() { delete [] data; }
};
类分配策略{
受保护的:
静态常数std::size\u t size=0;
};
样板
类静态:分配策略{
受保护的:
静态常数std::size\u t size=size;
};
类动态:分配策略{
受保护的:
静态常数std::size\u t size=0;
};
样板
类向量:TAllocationPolicy{
静态断言(!std::is_same::value&&std::is_base_of::value,“TAllocationPolicy必须继承自AllocationPolicy”);
使用TAllocationPolicy::Size;
公众:
T数据[大小];
};
样板
类向量:私有动态{
T*数据;
公众:
向量(std::size\u t size):数据(新的t[size]){}
~Vector(){delete[]data;}
};
您想要的所有功能都作为标准提供,或者可以插入现有的标准扩展点
尽可能在堆栈上使用静态分配(避免调用new以提高效率)
见面。它是C数组上的C++包装,并呈现了所有相同的特性。
如果用户希望提供先前分配的数组,则可以从指针实例化
会见分配者。您可以编写一个满足要求的分配器,该分配器返回已分配的内存,然后简单地使用。这种分配器正在考虑将来的标准以及其他分配器增强功能,如多态分配器
该类需要轻松地转换为简单指针。这允许使用以前用C编写的例程
两者都有,这只是一件小事
如果您想在运行时提供此选项,请考虑<代码> Boo::变体< /代码>。滚动您自己的歧视联盟-不建议。
您想要的所有功能都作为标准提供,或者可以插入现有的标准扩展点
尽可能在堆栈上使用静态分配(避免调用new以提高效率)
见面。它是C数组上的C++包装,并呈现了所有相同的特性。
如果用户希望提供先前分配的数组,则可以从指针实例化
会见分配者。您可以编写一个满足要求的分配器,该分配器返回已分配的内存,然后简单地使用。这种分配器正在考虑将来的标准以及其他分配器增强功能,如多态分配器
该类需要轻松地转换为简单指针。这允许使用以前用C编写的例程
两者都有,这只是一件小事
如果您想在运行时提供此选项,请考虑<代码> Boo::变体< /代码>。滚动您自己的有区别的联合-不建议。
您正在谈论两种定位数据的策略:作为小数组优化的内联,或通过指向动态分配缓冲区的指针的间接寻址
有两种方法可以选择策略:使用静态类型信息,或动态选择。动态选择需要存储来指示任何特定向量是使用静态策略还是动态策略
对于双精度向量,您可能可以在fir中使用非法值