maidamai
maidamai

Reputation: 712

Using type alias in template with the same type with template class it self

I don't understand some code in singly_linked.hpp:

template <class T>
struct singly_linked {
  // -- member types -----------------------------------------------------------

  /// The type for dummy nodes in singly linked lists.
  using node_type = singly_linked<T>;  // exactly type of node_type???

  /// Type of the pointer connecting two singly linked nodes.
  using node_pointer = node_type*;

  // -- constructors, destructors, and assignment operators --------------------

  singly_linked(node_pointer n = nullptr) : next(n) {
    // nop
  }

  // -- member variables -------------------------------------------------------

  /// Intrusive pointer to the next element.
  node_pointer next;
};

what is the exactly type of node_type? Does it causes a infinite loop? For example if I have:

singly_linked<int> node;

then what is the type of singly_linked<int>::node_type?

Upvotes: 0

Views: 62

Answers (1)

Eugene
Eugene

Reputation: 7188

You misunderstand meaning of using node_type = singly_linked<T>;. It does not declare a variable of type singly_linked<T> (which would indeed lead to infinite recursion and would cause a compiler error). Instead, this introduces an alias for this type: singly_linked<T>.

Thus, asking about type of singly_linked<int>::node_type does not make sense, since it is a type itself.

Upvotes: 1

Related Questions