【问题标题】:partial specialization with inheritance. Can I avoid inheritance?带有继承的部分特化。我可以避免继承吗?
【发布时间】:2014-05-07 19:13:48
【问题描述】:

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

  1. 尽可能在堆栈上使用静态分配(以避免调用 new 以提高效率)。
  2. 如果用户更愿意提供以前分配的数组,则可以从指针实例化。
  3. 类需要轻松转换为简单指针。这允许使用以前在 C 中编写的例程。

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

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

我喜欢下面的实现。我想将我想出的界面保留在 main.js 中。

我不喜欢 sizeof(Vector3) = 32 在 C 中,三个双精度的向量是 24 字节。原因是虚拟表多出了 8 个字节。

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

总结:

  1. 我想要一个 24 字节的 Vector3,就像在 C 中一样。
  2. 我仍然想要任意大的向量(使用<double,n>
  3. 我想保留 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


【解决方案1】:

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

尽可能在堆栈上使用静态分配(以避免调用 new 以提高效率)。

std::array&lt;T, N&gt;。它是 C 数组上的 C++ 包装器,具有所有相同的特征。

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

认识分配器。您可以编写一个满足返还已分配内存要求的分配器,然后只需使用std::vector。这种分配器正在考虑用于未来的标准以及其他分配器增强功能,例如多态分配器。

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

std::vectorstd::array 都将其视为琐碎。

如果您想在运行时提供此选择,请考虑boost::variant。滚动您自己的歧视性工会 - 不建议。

【讨论】:

  • 提醒:std::array 仅适用于 C++11 及更高版本。许多商店仍在使用旧版本。
  • @ThomasMatthews:std::array 用 C++03 编写绝对简单,并且在 Boost 中也可用。
  • 尝试阅读 OP 的帖子。他已经包含了 C++11 的静态断言和对 C++11 特性的依赖。
  • 我之前研究过这些选项。你能看看主界面吗?你认为你可以提供一个可以满足我对该接口要求的 STL 实现吗?这就是我要问的所有@jeffrey,请把个人喜好放在一边。
  • 提醒是给除了OP以外的其他人的。 :-)
【解决方案2】:

如果我的理解正确,LLVM's SmallVector 之类的内容似乎符合要求。它有一个模板参数,声明您希望在堆栈上分配的最大大小,并且仅当它超出该范围时才切换到堆内存。

如果它不直接适合您的界面,我相信查看实现对于您自己编写类似的东西非常有用。

【讨论】:

  • 这看起来很有趣。谢谢你。我会调查的。
【解决方案3】:

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

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

对于双精度向量,您也许可以在第一个元素中使用非法值(NaN 编码)来指示动态策略有效(指针需要与其余元素重叠存储,请使用联合这个)。

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

您可能想查看“小字符串优化”的实现。当数据小到可以直接存储在对象内部时,他们会为改进的引用局部性做出同样的权衡,并且通常会尽量避免使用不必要的模式空间。

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

【讨论】:

    【解决方案4】:

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

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

    1. 奇怪的反复出现的模板模式。
    2. 部分专业化
    3. 特征
    4. 使用模板进行编译时选择(看看我如何决定使用什么点积实现)。

    再次感谢,请发表评论!!见下面的代码

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

    【讨论】:

      【解决方案5】:

      您可以将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; }
      };
      

      【讨论】:

      • 这是一个糟糕的主意,我什至无法用语言来表达。
      • 拥有一个为两种不同实现提供单个容器接口的类,如果在编译时知道大小,则提供性能优势是一个莫名其妙的坏主意?
      • 或者您想知道是否有人需要容器的Size 来精确地成为对象的最大可能大小?不管怎样,你已经引起了我的兴趣。
      • 他指的是你选择的 -1 作为一个特殊的魔法值。
      • 我不应该说 -1 的 std::size_t 保证至少与对象的最大可能大小一样大。我不明白这怎么会导致问题。
      猜你喜欢
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2019-02-04
      • 1970-01-01
      • 2011-04-14
      相关资源
      最近更新 更多