title: Nested C++ template parameters for functions
body: <p>I want to have a templated function in C++, where one template parameter is itself a template of another template parameter. If that doesn't make any sense, take the following code that prints a std::vector that is templated on type T</p>
  <pre><code>template &lt;typename T&gt;
  void print_vector(std::vector&lt;T&gt; &amp;vec)
  {
      for(auto v: vec)
          std::cout &lt;&lt; v &lt;&lt; &quot; &quot;;
      std::cout &lt;&lt; std::endl;
  }
  ...
  std::vector&lt;double&gt; vec(5);
  ...
  print_vector(vec);
  </code></pre>
  <p>I want to further generalize this function for <a href="https://en.wikipedia.org/wiki/Standard_Template_Library" rel="noreferrer">STL</a> containers other than <code>vector</code>. But I don't know how to &quot;nest&quot; the template parameters such that the container is templated on type T. I have tried the following with no success</p>
  <pre><code>template &lt;typename T, template &lt;typename TT&gt; V&gt;
  void print_container(V&lt;T&gt; &amp;con)
  {
      for(auto c: con)
          std::cout &lt;&lt; c &lt;&lt; &quot; &quot;;
      std::cout &lt;&lt; std::endl;
  }
  ...
  std::vector&lt;double&gt; vec(5);
  ...
  print_container(vec);
  </code></pre>
  <p>I am sure this has been answered here before, but I can't find the search terms to find the answer.</p>
  <hr/>
  <p>Thanks @ForEveR. Your response was right on the money! All of the responses to my question observed that there is no need have the &quot;storage&quot; type T templated, with the following solution being adequate for the example I gave:</p>
  <pre><code>template &lt;typename C&gt;
  void print_container(C &amp;con)
  {
      for(auto v: con)
          std::cout &lt;&lt; v &lt;&lt; &quot; &quot;;
      std::cout &lt;&lt; std::endl;
  }
  </code></pre>
  <p>Unfortunately, the actual use case that motivated the question was a little bit more complicated. The routine takes multiple containers, like this linear algebra example with a matrix and vector class:</p>
  <pre><code>template &lt;typename MATRIX, typename VECTOR&gt;
  void mat_vec_multiply(const MATRIX &amp;A, const VECTOR &amp;x, VECTOR &amp;y)
  {
      // Implement y = A*x;
  }
  </code></pre>
  <p>Assume that both the MATRIX and VECTOR classes have to be templated on the same underlying storage class (i.e., <code>double</code>, <code>float</code>, <code>int</code>, etc.). The idea is that by explicitly specifying T as a template parameter, we can enforce this:</p>
  <pre><code>template &lt; typename T,
             template&lt;typename&gt; class MATRIX,
             template&lt;typename&gt; class VECTOR&gt;
  void mat_vec_multiply(const MATRIX&lt;T&gt; &amp;A, const VECTOR&lt;T&gt; &amp;x, VECTOR&lt;T&gt; &amp;y)
  {
      // Implement y = A*x;
  }
  </code></pre>
  <p>Unfortunately, I am using the <a href="https://en.wikipedia.org/wiki/CUDA" rel="noreferrer">CUDA</a> compiler nvcc, which doesn't have any support for C++11 constructs (I just used C++11 in my example because it is less verbose). So I can't use <code>std::is_same</code> and <code>static_assert</code>, though I suppose I could roll my own <code>is_same</code> (or use <a href="https://en.wikipedia.org/wiki/Boost_%28C%2B%2B_libraries%29" rel="noreferrer">Boost</a>) easily enough. What is the &quot;best practice&quot; in this case, where I want to enforce the common template parameter for the storage classes?</p>