展平/重构C++某些输入的模板源

Flatten / Refactor C++ template source for certain inputs

本文关键字:输入 重构 C++ 展平      更新时间:2023-10-16

我有一些模板代码正在尝试重构。 具体来说,它是一种几何类型,按参数尺寸模板化(因此它可以表示曲线、曲面、体积、超体积等(以及点类型。

问题在于,以这种通用的方式进行编辑变得非常笨拙,而且大多数时候我们只使用参数尺寸 1、2 和 3。部分专业化是这些天唯一改变的东西,共享代码非常稳定和完整。

除了编辑代码的困难之外,还有一些性能问题,这些问题源于必须以通用到任意维度网格的方式存储内部数据......可以以通用方式缓解性能问题,但这只会继续增加许多不必要的复杂性。 基本上问题是模板太笼统了。

因此,我将用 3 个单独的模板替换通用模板代码,每个维度一个。 我还想保留点类型的模板,所以我不想只使用普通类。

如果我多次使用宏和 #including 文件完成 C 方式模板化,我可以通过预处理器运行输入并获得我想要的 3 个不同版本。

虽然我可以手动完成,但我更喜欢自动化解决方案,至少作为起点。

是否有任何类似的方法或重构解决方案可用于C++,以获取具有特定输入的模板的源代码?


更具体一点,我有这样的代码:

template< int Dimension, class PointType > class Nurbs { ... }
template< class PointType > class NurbsCurve : public Nurbs< 1, PointType > { ... }
template< class PointType > class NurbsSurface : public Nurbs< 2, PointType > { ... }
template< class PointType > class NurbsVolume : public Nurbs< 3, PointType > { ... }

但我想以这样的代码结束:

template< class PointType > class NurbsCurve { ... }
template< class PointType > class NurbsSurface { ... }
template< class PointType > class NurbsVolume { ... }

这并不能真正回答您的问题,但这是保留模板化代码的另一种方法。

如果我理解正确,您的代码具有如此多的专用性,以至于变得笨拙。处理此问题的一种方法是使用一些帮助程序模板类来处理所有详细信息,并从其他模板调用其静态成员。帮助程序类将具有实现泛型(与维度无关的代码(的基本内容,然后将有专用化,这些专用化仅覆盖需要为特定维度重写的任何内容。

namespace details {
  template<int Dimension> struct helper_base  // generic code
  {
    static_assert(Dimension>1,"missing specialisation Dimension=0,1");
    static const int Last = Dimension-1;
    template<typename T>
    static T dot_product(const T*a, const T*b) noexcept
    { return helper<Last>::dot_product(a,b) + a[Last]*b[Last]; }
  };
  template<> struct helper_base<1>
  {
    template<typename T>
    static T dot_product(const T*a, const T*b) noexcept
    { return a[0]*b[0]; }
  };
  template<int Dimension> struct helper // special code for certain dimensions
    : helper_base<Dimension> {};
  template<> struct helper<3> : helper_base<3>
  {
    // any code that is particular to 3D.
    template<typename T>
    static void cross_product(T*p, const T*x, const T*y) noexcept
    {
      p[0] = x[1]*y[2] - x[2]*y[1];
      p[1] = x[2]*y[0] - x[0]*y[2];
      p[2] = x[0]*y[1] - x[1]*y[0];
    }
  };
}
template<typename T, int Dimension>
struct point
{
   using helper = details::helper<Dimension>;
   T X[Dimension];  // for instance
   T operator*(point const&x) const noexcept { return helper::dot_product(X,x.X); }
   // etc.
};
template<typename T>
point<T,3> operator^(point<T,3> const&x, point<T,3> const&y) noexcept
{
  point<T,3> result;
  details::helper<3>::cross_product(result.X,x.X,y.X);
  return result;
}

不确定它回答了你的问题:

您可以删除继承并使用成员,而不是:

template<class PointType> class NurbsCurve   : public Nurbs<1, PointType> { ... };
template<class PointType> class NurbsSurface : public Nurbs<2, PointType> { ... };
template<class PointType> class NurbsVolume  : public Nurbs<3, PointType> { ... };

使用类似以下内容:

template<class PointType> class NurbsCurve   { ... private: Nurbs<1, PointType> data; };
template<class PointType> class NurbsSurface { ... private: Nurbs<2, PointType> data; };
template<class PointType> class NurbsVolume  { ... private: Nurbs<3, PointType> data; };

注意:- 您可能需要在每个类中复制Nurbs的原型。- 稍后如果需要,您可以通过特定的实现替换 Nurbs。