title: C++ Templated class function that can detect std::vector
body: <p>If I have a templated class, I can do the following to detect if a vector was passed:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>template&lt;typename T&gt; struct is_vector { static const bool value=false; };
  template&lt;typename T&gt; struct is_vector&lt;std::vector&lt;T&gt;&gt; { static const bool value=true; };
  
  template&lt;class T&gt;
  class Parser {
  public:
      Parser() {}
      void parse(T obj) {
          if (is_vector&lt;T&gt;::value) {
              std::cout &lt;&lt; "vector\n";
  
              //obj.push_back(T {});
          }
          else {
              std::cout &lt;&lt; "not vector\n";
          }
      }
  };
  
  int main() {
      Parser&lt;int&gt; p1;
      p1.parse(123);
  
      Parser&lt;std::vector&lt;int&gt;&gt; p2;
      p2.parse({ 1, 2, 3});
  
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>not vector
  vector
  </code></pre>
  
  <p>I can detect a vector, yet the compiler complains when I uncomment the <code>push_back</code> call:</p>
  
  <pre><code>main.cpp: In instantiation of ‘void Parser&lt;T&gt;::parse(T) [with T = int]’:
  main.cpp:26:14:   required from here
  main.cpp:15:17: error: request for member ‘push_back’ in ‘obj’, which is of non-class type ‘int’
               obj.push_back(T {});
               ~~~~^~~~~~~~~
  </code></pre>
  
  <p>Obviously, an int does not have a <code>push_back</code> function, but the vector does. The <code>is_vector</code> call is evaluated at runtime, but the <code>push_back</code> is caught at compile time.</p>
  
  <p>With partial template specialization, I can do what I want:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>template&lt;typename T&gt;
  void parse(T obj) {
      std::cout &lt;&lt; "not vector: " &lt;&lt; obj &lt;&lt; "\n";
  }
  
  template&lt;typename T&gt;
  void parse(std::vector&lt;T&gt; obj) {
      std::cout &lt;&lt; "is vector\n";
      for (auto i : obj) std::cout &lt;&lt; i &lt;&lt; " ";
  
      obj.push_back(T {});
  
      std::cout &lt;&lt; "\n";
      for (auto i : obj) std::cout &lt;&lt; i &lt;&lt; " ";
      std::cout &lt;&lt; "\n";
  }
  
  int main() {
      parse(1);
      parse('a');
      parse(std::vector&lt;int&gt; { 1, 2, 3 });
  
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>not vector: 1
  not vector: a
  is vector
  1 2 3 
  1 2 3 0 
  </code></pre>
  
  <p>So, how can I combine these 2 ideas, either at compile-time or at runtime? That is, have a templated class with a function that can handle vectors and non-vectors?</p>