带有继承的部分专门化.我能避免继承吗?

partial specialization with inheritance. Can I avoid inheritance?

本文关键字:继承 能避免 专门化      更新时间:2023-10-16

我正在编写一个向量类,我希望它具有以下特征:

  1. 尽可能在堆栈上使用静态分配(避免为了效率调用new)。
  2. 如果用户希望提供先前分配的数组,则可以从指针实例化。
  3. 该类需要很容易地转换为一个简单的指针。这允许使用c中先前编写的例程。

找到下面这个简单的测试问题与我想出的解决方案。我使用继承,所以Vector从Vector_base继承,Vector_base为所有Vector提供了一个公共接口(纯虚拟)。然后我定义了一个空类Vector,允许我使用部分专门化来拥有不同的存储方案;静态或动态

这背后的想法是,我只是想让vector成为老式静态数组的c++包装器。

我喜欢下面的实现。我想保留main中我设计的接口。

我不喜欢的是sizeof(Vector3) = 32,而在C中,三个双精度向量是24字节。这样做的原因是额外的8字节的虚拟表。

我的问题:我能想出另一种设计,将为我提供相同的接口,但向量只有24个字节?

总结:

  1. 我想有一个24字节的Vector3,在C.
  2. 我仍然希望有任意大的向量(与<double,n>)
  3. 我想保留main()中使用的接口

我可以使用像trait或policy这样的编程习惯吗?我对这些很陌生,我不知道他们是否能提供解决方案。

在下面找到我的小测试代码:

#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;
}

您想要的所有功能都作为标准提供,或者可以插入到现有的标准扩展点。

尽可能在堆栈上使用静态分配(避免为了效率而调用new)。

遇见std::array<T, N>。它是C数组上的c++包装器,并呈现所有相同的特征。

如果用户希望提供先前分配的数组,则可以从指针实例化。

满足分配器。您可以编写一个分配器,满足返回已经分配的内存的要求,然后简单地使用std::vector。这样的分配器正在考虑用于未来的标准,以及其他分配器增强,如多态分配器。

类需要容易地转换为一个简单的指针。这允许使用以前在c中编写的例程。

std::vectorstd::array都提供了这一点。

如果您想在运行时提供此选择,请考虑boost::variant。组建你自己的受歧视的工会——不建议。

如果我理解正确的话,像LLVM的SmallVector这样的东西似乎符合要求。它有一个模板参数,声明你想在堆栈上分配的最大大小,只有当它超出这个范围时才切换到堆内存。

如果它不直接适合你的接口,我敢肯定,看看它的实现对于你自己编写类似的东西是非常有用的。

您正在谈论两种定位数据的策略:内联作为小数组优化,或通过间接指向动态分配的缓冲区的指针。

有两种方法可以做出这个策略选择:使用静态类型信息,或者动态的。动态选择需要存储来指示任何特定的向量是使用静态策略还是动态策略。

对于双精度类型的vector,您可以在第一个元素中使用非法值(NaN编码)来表示动态策略生效(指针需要与其余元素重叠存储,为此使用联合)。

但是在其他数据类型中,所有可能的位模式都是有效的。对于那些您将需要额外的存储空间来选择策略。您可能知道,对于特定的问题,特定的位不需要用于值的范围,并且可以用作标志。但是没有一个通用的解决方案适用于所有的数据类型。

你可能想看看"小字符串优化"的实现。当数据足够小,可以直接存储在对象中时,它们也会做出同样的权衡,以改善引用的局域性,并且通常会尽量避免使用模式空间,除非必要。

有一件事是肯定的。为了避免空间需求的显著增加,您将需要紧密耦合。没有专门化,没有继承,只有一个实现这两个策略的整体类。

好了。这花了我一整天的时间,但这是我想出的解决方案,它正是我想要的。请分享您对这个解决方案的意见和建议。当然,我没有实现我想要的所有方法。我只实现了两个伪点积,以显示在编译时如何通过使用模板选择特定的C实现。

这个方案比我想象的要复杂得多。我用来完成我的设计需求的基本概念是:

  1. 奇怪的重复模板模式。
  2. 局部专门化
  3. <
  4. 特征/gh>编译时选择模板(看看我如何决定使用什么点积实现)。
再次感谢,请评论!!参见下面的代码

#include <iostream>
using namespace std;
#include <type_traits>
//C++11 typedefs to create more specialized three-dimensional vectors.                                                                                                                                                                                                                                                       
#if (__cplusplus<201103L)
#error A compiler with the C++2011 standard is required!
#endif
template<class T>
struct traits{};
#define TRACE0(a) cout << #a << endl; a;
#define TRACE1(a) cout << #a "=[" << a << "]" << endl;
enum {Dynamic = -1};
template<typename T,int n>
struct mem_model{
  typedef T array_model[n];
};
//Specialization to Dynamic                                                                                                                                                                                                                                                                                                  
template<typename T>
struct mem_model<T,Dynamic>{
  typedef T* array_model;
};
template<class derived_vector>
struct Vector_base: public traits<derived_vector>{ //With traits<derived_vector> you can derive the compile time specifications for 'derived_vector'                                                                                                                                                                         
  typedef traits<derived_vector> derived;
  typedef typename traits<derived_vector>::Scalar Scalar;
public:
  inline int size()const{ //Calling derived class size in case a resize is done over a dynamic vector                                                                                                                                                                                                                        
    return static_cast<const derived_vector*>(this)->size(); //derived_vector MUST have a member method size().                                                                                                                                                                                                              
  }
  inline operator Scalar*(){return a;} //All vectors reduce to a Scalar*                                                                                                                                                                                                                                                     
  inline bool IsStatic()const{return (n==Dynamic)? false: true;}
  inline int SizeAtCompileTime()const{return n;} //-1  for dynamic vectors                                                                                                                                                                                                                                                   
protected:
  using derived::n; //compile time size. n = Dynamic if vector is requested to be so by the user.                                                                                                                                                                                                                            
  typename mem_model<Scalar,n>::array_model a;  //All vectors have a Scalar* a. Either static or dynamic.                                                                                                                                                                                                                    
};
//Default static                                                                                                                                                                                                                                                                                                             
template<typename Scalar,int n>
class Vector:public Vector_base<Vector<Scalar,n> >{ //Vector inherits main interface from Vector_base                                                                                                                                                                                                                        
public:
  //Constructors                                                                                                                                                                                                                                                                                                             
  Vector(){
    //do nothing for fast instantiation                                                                                                                                                                                                                                                                                      
  }
  Vector(const Scalar& x,const Scalar& y,const Scalar& z){
    a[0] = x; a[1] = y; a[2] = z;
  }
  //                                                                                                                                                                                                                                                                                                                         
  inline int size()const{return n;}
private:
  using Vector_base<Vector<Scalar,n> >::a;
};
//Traits specialization for Vector. Put in an inner_implementation namespace                                                                                                                                                                                                                                                 
template<typename _Scalar,int _n>
struct traits<Vector<_Scalar,_n> >{
  typedef _Scalar Scalar;
  enum{
    n = _n
  };
};
double clib_dot_product_d(const int n,double* a,double* b){
  double dot = 0.0;
  for(int i=0;i<n;i++)
    dot += a[i]*b[i];
  return dot;
}
float clib_dot_product_f(const int n,float* a,float* b){
  cout << "clib_dot_product_f" << endl;
  return 1.0;
}
template<typename Scalar>
struct dot_product_selector{};
template<>
struct dot_product_selector<double>{
  template<class derived1,class derived2>
  static double dot_product(Vector_base<derived1> &a,Vector_base<derived2> &b){
    return clib_dot_product_d(a.size(),a,b);
  }
};
template<>
struct dot_product_selector<float>{
  template<class derived1,class derived2>
  static float dot_product(Vector_base<derived1> &a,Vector_base<derived2> &b){
    return clib_dot_product_f(a.size(),a,b);
  }
};
template<class derived1,class derived2>
typename Vector_base<derived1>::Scalar dot_product(Vector_base<derived1> &a,Vector_base<derived2> &b){
  //run time assert checking the two sizes are the same!!                                                                                                                                                                                                                                                                    
  //Compile time (templates) check for the same Scalar type                                                                                                                                                                                                                                                                  
  static_assert( std::is_same<typename Vector_base<derived1>::Scalar,typename Vector_base<derived2>::Scalar>::value,"dot product requires both vectors to have the same Scalar type");
  return dot_product_selector<typename Vector_base<derived1>::Scalar>::dot_product(a,b);
}
#if 0
template <typename Scalar,alloc_type flg=Static>
using Vector3 = Vector<Scalar,3,flg>;
#endif
int main(){
  cout << "Testing Vector3: " << endl;

  Vector<double,3> as;
  Vector<double,Dynamic> ad;
  TRACE1(sizeof(as));
  TRACE1(sizeof(ad));
  TRACE1(as.SizeAtCompileTime());
  TRACE1(ad.SizeAtCompileTime());
  Vector<double,3> u(1,2,3),v(-1,1,5);
  Vector<float,3> uf,vf;
  TRACE1(dot_product(u,v));
  dot_product(uf,vf);
  //dot_product(u,vf); //this triggers a compile time assertion using static_assert                                                                                                                                                                                                                                          
  return 0;
}

您可以将Vector模板专门化简化为…

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
};

实现可能是围绕std::vectorstd::array的薄包装。

编辑:这避免了魔术常数…

template<typename T = void>
class Structure {};
template<typename T, std::size_t Size>
class Structure<T[Size]> {
    T data[Size];
    // The statically allocated implementation
};
template<typename T>
class Structure<T[]> {
    T * pData;
public:
    Structure(std::size_t size) : pData(new T[size]) {}
    ~Structure() { delete[] pData; }
    // The dynamically allocated implementation
};

像这样实例化…

Structure<int[]> heap(3);
Structure<int[3]> stack;

编辑:或者使用这样的策略…

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; }
};