7

I define a Node class with template for its value type

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

Most times, the node value of interest will be an object class, say class Foo. In that case, use Node<Foo *> will be more convenient. But it could also be that the node will hold primitive time, say int. Then use Node<int> will suffice.

The problem is, some of the function may need to behave differently based on whether T is a pointer type or not. For example, print should cout << *v when it is and cout << v otherwise.

What I've tried is to define 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; }
}

It now can choose the appropriate definition based on whether it's Node<int> or Node<int *> But the problem become, the two definitions will share many code. I'm wondering whether there's better way to achieve this.

Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
Oxdeadbeef
  • 1,033
  • 2
  • 11
  • 26
  • Can you explain why you think Node would be more convenient than Node? If this Node is being used as a tree-node for example, then Node* would be a better solution – dchhetri Jan 25 '13 at 20:08
  • Foo are globally allocated/used objects. Only passing Foo* to the data field won't involve recreating the objects. I'm only saying the type T for Node, not Node itself. – Oxdeadbeef Jan 25 '13 at 23:10
  • Hi, did you solve that problem? I am in the same situation, where I want just different behave of destructor. Specially, if is parametr value, I just want to delete node, but if it is pointer, I want first delete that pointer and after that the node self. – Patrik Valkovič Nov 20 '16 at 15:51

2 Answers2

4

See this: C++ template specialization, calling methods on types that could be pointers or references unambiguously

The same technique should work here, allowing you to deal with the val as a reference (or a pointer) uniformly in both cases.

CRTP may help reduce code duplication, allowing for common code for two specializations without any overhead, as well.

Note that ownership semantics get tricky when you sometimes use a pointer and sometimes an instance -- what is the lifetime of val if sometimes it is a pointer of an argument, and other times it is a copy of the argument, and how to you enforce it?

Community
  • 1
  • 1
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
2

Well, there is one more way of doing this. You should use type traits, they are evaluated at compile time. This is how you can modify.

template<class T>
class Node {
  T val;
  public:
    Node (T & v) : val (v) {}
    ...
    void print() { 
      if(std::is_pointer<T>::value)
        cout << *v << endl;
      else
        cout << v << endl;
    }
}
Alok
  • 127
  • 1
  • 7