2

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

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

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

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

如果我已经用宏和#include 文件多次模板化 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 { ... }
4

2 回答 2

0

不确定它是否回答了您的问题:

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

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。

于 2013-09-26T11:20:11.443 回答
0

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

如果我理解正确,您的代码有很多专业化,以至于它变得笨拙。解决这个问题的一种方法是使用一些帮助模板类来处理所有细节,并从其他模板调用它的静态成员。辅助类将有一个实现泛型(与维度无关的代码)的基础,然后会有专门化,只覆盖特定维度需要覆盖的任何内容。

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;
}
于 2013-09-26T09:57:14.877 回答