模板对象作为模板类的静态成员

发布于 2024-09-06 23:19:39 字数 904 浏览 8 评论 0原文

想象一下下面的模板类(省略了成员_t的setter和getter):

template<class T>
class chain
{
public:
  static chain<T> NONE;

  chain()
   : _next(&NONE)
  {}

  ~chain() {}

  chain<T>& getNext() const
  {
    return *_next;
  }

  void setNext(chain<T>* next)
  {
    if(next && next != this)
      _next = next;
  }

  chain<T>& getLast() const
  {
    if (_next == &NONE)
      return *this;
    else
      return _next->getLast();
  }

private:
  T _t;
  chain<T>* _next;
};

这个概念的基本思想是,我不使用空指针,而是有一个静态默认元素来承担这个角色,而仍然是技术上有效的对象;这可以防止空指针的一些问题,同时使代码更加冗长...

我可以很好地实例化此模板,但链接器在静态上给出了 unresolved-external 错误成员对象NONE

我假设在实例化模板时,行 static chain;无;实际上也将是一个定义,因为它实际上发生在实例化模板的实现中。然而,事实证明并非如此......

我的问题是:是否有可能,如果可以,如何在每个模板实例化之前不显式定义 NONE 元素?

Imagine the following template class (setter and getter for the member _t omitted):

template<class T>
class chain
{
public:
  static chain<T> NONE;

  chain()
   : _next(&NONE)
  {}

  ~chain() {}

  chain<T>& getNext() const
  {
    return *_next;
  }

  void setNext(chain<T>* next)
  {
    if(next && next != this)
      _next = next;
  }

  chain<T>& getLast() const
  {
    if (_next == &NONE)
      return *this;
    else
      return _next->getLast();
  }

private:
  T _t;
  chain<T>* _next;
};

The basic idea of this concept is, instead of using null-pointers, I have a static default element that takes in this role while still being a technically valid object; this could prevent some of the issues with null pointers while making the code more verbose at the same time...

I can instantiate this template just fine, but the linker gives an unresolved-external error on the static member object NONE.

I would have assumed that when instantiating the template, the line static chain<T> NONE; would effectively be a definition, too, as it actually happens within the implementation instantiating the template. However, it turns out not to be...

My question is: is something like possible at all, and if so, how, without explicitly defining the NONE element before each and every template instantiation?

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

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

发布评论

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

评论(2

厌味 2024-09-13 23:19:39

您仍然需要像非模板类一样在类外部定义它。就像在非模板类中一样,您只在类定义中声明了 NONE 并且仍然需要定义它:

template<class T>
class chain
{
    // the same as your example
};

// Just add this
template <class T>
chain<T> chain<T>::NONE;

You still need to define that outside the class just like a non-template class. Just like in a non-template class, you have only declared NONE inside the class definition and still need to define it:

template<class T>
class chain
{
    // the same as your example
};

// Just add this
template <class T>
chain<T> chain<T>::NONE;
忆梦 2024-09-13 23:19:39
template < typename T >
chain<T> chain<T>::NONE;

应该有效

template < typename T >
chain<T> chain<T>::NONE;

should work

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