title: How to enable conversion template argument T to const T?
body: <p>Suppose that I have a following class</p>
  
  <pre><code>template &lt;typename T&gt;
  struct Node { T value; Node* next; };
  </code></pre>
  
  <p>Often one needs to write code similar to this (let's assume that Sometype is std::string for now, although I don't think that it matters). </p>
  
  <pre><code>Node&lt;SomeType&gt; node = Node{ someValue, someNodePtr };
  ...
  Node &lt;const SomeType&gt; constNode = node; // compile error
  </code></pre>
  
  <p>One way to work around is to define explicit conversion operator:</p>
  
  <pre><code>template &lt;typename T&gt;
  struct Node
  {
      T value;
      Node* next;
      operator Node&lt;const T&gt;() const { 
          return Node&lt;const T&gt;{value, reinterpret_cast&lt;Node&lt;const T&gt;* &gt;(next)};
      }
  };
  </code></pre>
  
  <p>Is there a better, "proper" way to do it? 
  1. In general, what is the proper way to allow conversion of SomeType to SomeType except explicitly defining conversion operator? (Not in my example only).
  2. If defining conversion operator is necessary, is reinterpret_cast is the proper way to do it? Or there are "cleaner" ways?</p>
  
  <p>EDIT: Answers and comments were very helpful. I decided to provide more context right now. My problem is not with implementing const_iterator itself (I think that I know how to do it), but how to use same template for iterator and const_iterator. Here is what I mean</p>
  
  <pre><code>template &lt;typename T&gt;
  struct iterator
  {
      iterator(Node&lt;T&gt;* _node) : node{ _node } {}
      T&amp; operator*() { return node-&gt;value; } // for iterator only
      const T&amp; operator*() const { return node-&gt;value; } // we need both for iterator 
                                                         // for const iterator to be usable
  
      iterator&amp; operator++() { node = node-&gt;next; return *this; }
      iterator operator++(int) { auto result = iterator{ node }; node = node-&gt;next; return result; }
  
      bool operator==(const iterator&amp; other) { return node == other.node; }
      bool operator!=(const iterator&amp; other) { return Node != other.node; }
  
  private:
      Node&lt;T&gt;* node;
  };
  </code></pre>
  
  <p>Implementing const_iterator is essentially the same, except that T&amp; operator*() { return node->value; }.</p>
  
  <p>The initial solution is just to write two wrapper classes, one with T&amp; operator*() and the other one without. Or use inheritance, with iterator deriving from const_iterator (which might be a good solution and has an advantage -  we don't need to rewrite comparison operators for iterator and can compare iterator with const_iterator - which most often makes sense - as we check that they both point at same node).</p>
  
  <p>However, I am curious how to write this without inheritance or typing same code twice. Basically, I think that some conditional template generation is needed - to have the method T&amp; operator*() { return node->value; } generated only for iterator and not const_iterator. What is the proper way to do it? If const_iterator treated the Node* as Node*, it almost solves my problem.</p>