title: How to restrict template parameter to pointer or random access iterator only?
body: <p>Is there a way to restrict the parameter type of a template function to only pointers or random-access iterators?</p>
  
  <p>Say I am developing a sorting function which works only with random accessible containers. I am looking for a way to throw a compile-time error if the user passes a non-random-access iterator.</p>
  
  <pre><code>#include &lt;type_traits&gt;
  #include &lt;iterator&gt;
  
  template &lt;class Iterator&gt; void mySort(Iterator begin, Iterator end){
  
      /*The below condition must be true if the 'Iterator' type is a pointer 
      or if it is of Category random_access_iterator_tag. How to make such check?*/
      static_assert(some condition, "The mySort() function only accepts random access iterators or raw pointers to an array.\n");
  
      for (Iterator it = begin; it != end; ++it){
          /*Some kind of sorting is performed here, which 
          uses arithmetic operators + and - in the iterator type. */
      }
  }
  </code></pre>
  
  <p>I know that to check if a type is a pointer I could use <code>std::is_pointer&lt;Iterator&gt;::value</code> and to check if the iterator is random access I could use <code>std::is_same&lt;std::random_access_iterator_tag, Iterator::iterator_category&gt;::value</code>.</p>
  
  <p>The first problem is that both checks should be OR'd inside the same <code>static_assert()</code>, otherwise if either one was matched, the other would not.</p>
  
  <p>The second problem is that the random access check would fail if the function was to be called called in such a way: <code>mySort&lt;int*&gt;(...)</code>. This obviously happens since <code>int*</code> does not have the <code>::iterator_category</code> definition.</p>
  
  <p>Does anybody have an idea how to solve this?
  I also know the compiler would automatically throw errors for the attempt of using arithmetic operators with non-random-access iterators, but I would like to display a more comprehensive error message through <code>static_assert()</code>.</p>
  
  <p>As a follow up question. In the case 'Iterator' is a pointer type, is there a way to assert it is of a primitive type (non <code>struct</code>/<code>class</code>)?</p>
  
  <p>Thank you in advance.</p>