Class template indicating pointer type and regular type

I define a Node class with a template for its value type

template<class T>
class Node {
  T val;
  public:
    Node (T & v) : val (v) {}
    ...
    void print() { cout << v << endl; }
}

In most cases, the Node value of interest will be an object class, for example class Foo. In this case, the use Node<Foo *>will be more convenient. But it may also be that Node will contain primitive time, say int. Then enough to use Node<int>.

The problem is that some of the functions may have to behave differently based on whether the Ttype of pointer is or not. For example, it printshould cout << *v, when it is, and cout << votherwise.

I tried to determine both:

template<class T>
class Node {
  T val;
  public:
    Node (T & v) : val (v) {}
    ...
    void print() { cout << v << endl; }
}

template<class T>
class Node<T*> {
  T* val;
  public:
    Node (T* v) : val (v) {}
    ...
    void print() { cout << *v << endl; }
}

, , Node<int> or Node<int *> , . , .

+5
1

: ++, ,

, val ( ) .

CRTP , - .

, , , - val, , , ?

+4

All Articles