typedef 模板声明的替代方案

发布于 2024-09-18 22:20:41 字数 358 浏览 2 评论 0原文

我正在尝试

namespace NTL
{
    typedef std::valarray vector;
}

通过标准 C++ 来完成。我知道这是不允许的,但我需要一种快速且简单的方法(无需重新实现所有函数、运算符、重载等)来获取模板 typedef。

我现在正在做一个模板类 Vector,它有一个 valarray 作为数据成员,但这需要我重载我的向量的所有数学函数(再次......因为 valarray 也这样做)。

有什么想法吗?谢谢!

PS:我可能需要在某个时候扩展 NTL::vector 的功能,并且将其合并到解决方案中的方法会非常棒。

I'm trying to accomplish

namespace NTL
{
    typedef std::valarray vector;
}

through standard C++. I know it's not allowed, but I need a quick and easy way (without reimplementing all functions, operators, overloads, etc.) to get a template typedef.

I am now doing a template class Vector which has a valarray as data member, but that will require me to overload all math functions for my vector (again... as valarray does it as well).

Any ideas? Thanks!

PS: I will probably need to extend the functionality of NTL::vector at some point, and a way to incorporate that in the solution would be awesome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半城柳色半声笛 2024-09-25 22:20:41

在 C++0x 中,这非常简单,但目前您可以通过两种方式解决该问题:通过元函数或滥用继承。

namespace NTL {
   // metafunction
   template <typename T>
   struct vector_1 {
      typedef std::valarray<T> type;
   };

   // inheritance abuse:
   template <typename T>
   struct vector_2 : std::valarray<T>
   {};
}
int main() {
   NTL::vector_1<double>::type var1; // type is std::valarray<double>
   NTL::vector_2<double> var2;       // type inherits from std::valarray<double>
}

第二种方法可以轻松扩展,但请注意,通常不建议从 STL 容器公开继承,因为它们不是为扩展而设计的。特别是因为它们没有虚拟析构函数,如果您的对象从指向 STL 容器的指针中删除,您最终可能会得到未定义的行为...

我建议您私有继承并带上基成员函数通过 using 声明进入作用域(比提供公共继承更好)。它将需要更多的样板代码,但您不需要为所有所需的接口提供转发方法。

顺便说一句,C++0x 的方式是:

namespace NTL {
   template <typename T>
   using vector = std::valarray<T>;
}

In C++0x that will be really simple, but for the time being you can approach the problem in two ways, through a metafunction or by abusing inheritance.

namespace NTL {
   // metafunction
   template <typename T>
   struct vector_1 {
      typedef std::valarray<T> type;
   };

   // inheritance abuse:
   template <typename T>
   struct vector_2 : std::valarray<T>
   {};
}
int main() {
   NTL::vector_1<double>::type var1; // type is std::valarray<double>
   NTL::vector_2<double> var2;       // type inherits from std::valarray<double>
}

The second approach can be extended easily, but note that in general it is not recommended to inherit publicly from STL containers, as they were not designed to be extended. In particular since they don't have virtual destructor you can end up with undefined behavior if your object is deleted from a pointer to the STL container...

I would advise you to inherit privately and bring the base member functions into scope by means of a using declaration (better than providing public inheritance). It will require more boiler-plate code, but you won't need to provide forwarding methods for all your desired interface.

BTW, the C++0x way would be:

namespace NTL {
   template <typename T>
   using vector = std::valarray<T>;
}
因为看清所以看轻 2024-09-25 22:20:41

您可以从 std::valarray 派生您的 NTL::vector 类:

namespace NTL {
  template <class T>
  class vector : public std::valarray<T>
  {
  public:
    vector() : std::valarray<T>() { }
    vector(const std::valarray<T>& other) : std::valarray(other) { }
    /* other constructors to match the std::valarray constructors */

    vector& operator=(const vector& );
    vector& operator=(const std::valarray<T>& );
  };
}

这样,NTL::vector 就会转换为 >std::valarray 在一系列操作的开始,但生成的 valarray 也可以再次转换为 NTL::vector

一个重要警告:由于 stl::valarray 不是为多态使用而设计的,因此如果尝试通过 删除 NTL::vector 对象,您将会遇到麻烦std::valarray 指针!

You could derive your NTL::vector class from std::valarray:

namespace NTL {
  template <class T>
  class vector : public std::valarray<T>
  {
  public:
    vector() : std::valarray<T>() { }
    vector(const std::valarray<T>& other) : std::valarray(other) { }
    /* other constructors to match the std::valarray constructors */

    vector& operator=(const vector& );
    vector& operator=(const std::valarray<T>& );
  };
}

This way, an NTL::vector gets converted to a std::valarray at the start of a sequence of operations, but the resulting valarray can also be converted to an NTL::vector again.

One big warning: As stl::valarray is not designed for polymorphic use, you will get into trouble if try to delete an NTL::vector object through a std::valarray pointer!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文