Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, it would be <code>std::vector</code>, and nothing else, because <code>vector</code> resides in the <code>std</code> namespace and you are asking for a <strong>template template parameter</strong>, while <code>std::vector&lt;int&gt;</code> is not a template anymore. Next, a <code>std::vector</code> actually takes <strong>two</strong> template parameters, one for the type and the other for the allocator:</p> <pre><code>template &lt; typename T, template&lt;typename, typename&gt; class ContainerType, typename Alloc = std::allocator&lt;T&gt; &gt; class Stack{ ContainerType&lt;T, Alloc&gt; container; // ... }; // usage: Stack&lt;int, std::vector&gt; s; </code></pre> <p>Now, this only enables containers with two template parameters as the underlying type, so you're better off with what the standard does: take it as a normal type:</p> <pre><code>template &lt;typename T, typename ContainerType&gt; class Stack{ ContainerType container; // ... }; // usage: Stack&lt;int, std::vector&lt;int&gt; &gt; s; </code></pre> <p>To ensure that the underlying type has the same <code>T</code>, you can do a fake "static assert", or if you have a C++0x enabled compiler, you can do an actual static assert:</p> <pre><code>#include &lt;tr1/type_traits&gt; // C++03 us std::tr1::is_same //#include &lt;type_traits&gt; // C++0x, use std::is_same template &lt;typename T, typename ContainerType&gt; class Stack{ typedef typename ContainerType::value_type underlying_value_type; typedef char ERROR_different_value_type[ std::tr1::is_same&lt;T, underlying_value_type&gt;::value ? 1 : -1 ] ContainerType container; // ... }; </code></pre> <p>This works because if <code>T</code> is different from the used container's <code>T</code>, it will be a <code>typedef char ERROR_different_vale_type[-1]</code>, and an array of negative size can't possibly exist, which causes a compiler error. :) Now, with C++0x you can just <code>static_assert</code> that:</p> <pre><code>#include &lt;tr1/type_traits&gt; // C++03 //#include &lt;type_traits&gt; // C++0x template &lt;typename T, typename ContainerType&gt; class Stack{ typedef typename ContainerType::value_type underlying_value_type; static_assert(std::tr1::is_same&lt;T, underlying_value_type&gt;::value, "Error: The type of the stack must be the same as the type of the container"); ContainerType container; // ... }; </code></pre> <p>For convenience, you can now specify a default template argument for the common case:</p> <pre><code>template &lt;typename T, typename ContainerType = std::vector&lt;T&gt;&gt; class Stack{ ContainerType container; // ... }; // usage: Stack&lt;int&gt; s; </code></pre> <p>And at this point you can just use <code>std::stack</code> which does exactly this (though it uses <code>std::deque</code> as the underlying type). :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload