Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>According to <a href="http://msdn.microsoft.com/en-us/library/c6cyy67b.aspx" rel="nofollow noreferrer">MSDN</a> the key differences between C# generics and C++ templates are:</p> <ul> <li>C# generics do not provide the same amount of flexibility as C++ templates. For example, it is not possible to call arithmetic operators in a C# generic class, although it is possible to call user defined operators.</li> <li>C# does not allow non-type template parameters, such as template C {}.</li> <li>C# does not support explicit specialization; that is, a custom implementation of a template for a specific type.</li> <li>C# does not support partial specialization: a custom implementation for a subset of the type arguments.</li> <li>C# does not allow the type parameter to be used as the base class for the generic type.</li> <li>C# does not allow type parameters to have default types.</li> <li>In C#, a generic type parameter cannot itself be a generic, although constructed types can be used as generics. C++ does allow template parameters.</li> </ul> <p>Yet there are cases where you can work around some of these issues by using extension methods.</p> <p>Possible with C++ generics, neither with C# generics nor Java generics: true <strong>template metaprogramming</strong> (turing complete at compile time).</p> <pre><code>#include &lt;iostream&gt; template&lt;unsigned U&gt; struct Fac{ enum { value = U * Fac&lt;U-1&gt;::value};}; template&lt;&gt; struct Fac&lt;0&gt;{ enum { value = 1};}; template&lt;unsigned U&gt; struct Fib{ enum {value = (Fib&lt;U-1&gt;::value + Fib&lt;U-2&gt;::value)};}; template&lt;&gt; struct Fib&lt;0&gt;{ enum {value = 0};}; template&lt;&gt; struct Fib&lt;1&gt;{ enum {value = 1};}; template&lt;unsigned U&gt; void show(){ show&lt;U-1&gt;(); std::cout &lt;&lt; "Fib(" &lt;&lt; U &lt;&lt; ")=" &lt;&lt; Fib&lt;U&gt;::value &lt;&lt; "\t" &lt;&lt; "Fac(" &lt;&lt; U &lt;&lt; ")=" &lt;&lt; Fac&lt;U&gt;::value &lt;&lt; std::endl; } template&lt;&gt; void show&lt;0&gt;(){} int main(int argc, char** argv){ show&lt;12&gt;(); } </code></pre> <p><a href="http://ideone.com/Gdf3W" rel="nofollow noreferrer">http://ideone.com/Gdf3W</a></p> <p><strong>Edit</strong></p> <p>The C++ standard hadn't had constraints on type parameters while <a href="http://msdn.microsoft.com/en-us/library/d5x73970%28VS.80%29.aspx" rel="nofollow noreferrer">C#</a> and <a href="https://stackoverflow.com/q/2081663/303290">Java</a> have. Boost has something like that (<a href="http://www.boost.org/doc/libs/1_36_0/libs/concept_check/concept_check.htm" rel="nofollow noreferrer">Boost Concept Check Library</a>). But since C++11 you now can use <a href="http://www.cplusplus.com/reference/type_traits/" rel="nofollow noreferrer"><code>&lt;type_traits&gt;</code></a> to get something similar.</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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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