title: Templated Function that works for iterators over raw pointers as well as iterators over unique_ptrs
body: <p>Let's say I have a template function that takes a const range (or better, begin- and end-iterators) of some kind of pointer-collections. 
  This function internally constructs a STL-container with pointers to reorganize the elements.</p>
  
  <p>Now I want to reuse this function for unique_ptr-collections as well. I somehow need to modify the template parameters or introduce a new wrapper or overload... but how?
  Is there any C++11 template magic, STL helper or boost helper?
  Following an example code:</p>
  
  
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;memory&gt;
  
  // Element Class
  class Foo { };
  
  // Take a range of elements, sort them internally by their addresses and print them in order    
  template &lt;typename FooIterator&gt;
  void print_sorted_addresses(FooIterator beginFoos, FooIterator endFoos)
  {
      // Sort them
      std::vector&lt;const Foo*&gt; elements(beginFoos, endFoos);
      std::sort(elements.begin(), elements.end());
      // Print them
      for(const auto&amp; e : elements)
          std::cout &lt;&lt; e &lt;&lt; std::endl;
  }
  
  int main() {
      std::vector&lt;Foo*&gt; raw_foos;
      std::vector&lt;std::unique_ptr&lt;Foo&gt;&gt; unique_foos;
  
      // Fill them
      for(int i=0; i&lt;10; i++) {
          std::unique_ptr&lt;Foo&gt; foo(new Foo());
          raw_foos.push_back(foo.get());
          unique_foos.push_back(std::move(foo));
      }
  
      print_sorted_addresses(raw_foos.cbegin(), raw_foos.cend());
      //print_sorted_Foos(unique_foos.cbegin(), unique_foos.cend()); // ERROR
  
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>The culprit seems to be the non-uniform behavior of raw pointers and smart pointers (<code>unique_ptr</code> in particular) for converting them both to raw pointers.
  This can either be circumvented via a dereferencing-cycle à la <code>std::addressof(*p)</code>, but this only has well-defined behavior if p is not <code>nullptr</code>.
  To reduce any runtime-checks I played with conditional templates and came up with the following:</p>
  
  
  
  <pre><code>template&lt;typename Ptr&gt; using RawPtr = typename std::pointer_traits&lt;Ptr&gt;::element_type*; 
  
  // raw pointers like int**, const char*, ...
  template&lt;typename Ptr&gt;
  typename std::enable_if&lt;std::is_pointer&lt;Ptr&gt;::value, RawPtr&lt;Ptr&gt;&gt;::type make_raw(Ptr ptr) { return ptr; }
  
  // smart pointers like unique_ptr, shared_ptr, ... 
  template&lt;typename Ptr&gt;
  typename std::enable_if&lt;!std::is_pointer&lt;Ptr&gt;::value, RawPtr&lt;Ptr&gt;&gt;::type make_raw(Ptr&amp; ptr) { return ptr.get(); }
  </code></pre>
  
  <p>This could be used in @tclamb's iterator, or in boost::transform_iterator as in @Praetorian's answer. But it still feels strange to build upon the specific get()-member of a smart-pointer implementation instead of the operator*-interface what makes a pointer a pointer.</p>