Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <code>template</code>s and generic programming wherever possible. Don't rely on <strong>any</strong> types if you don't have to!</p> <p>If you have a function that takes a number and returns it multiplied by 2, write it like this:</p> <pre><code>template &lt;typename Number&gt; inline Number doubleNum(Number n) { return 2*n; } </code></pre> <p>Or even this:</p> <pre><code>template &lt;typename RetType, typename NumberType&gt; inline RetType doubleNum(NumberType n) { return 2*n; } </code></pre> <p>This way, if you have a library that uses <code>int</code>s, <code>double</code>s, <code>uint64_t</code>s - you name it - you can work with it, without rewriting your code. If you need to work with binary files or network programming, you can work with fixed-size types, without rewriting your code. And if you need arbitrary precision numbers, you can work with a class which implements the same interface as a primitive integer or float type via operator overloading, such as a GMP wrapper, <em>without rewriting your code</em>.</p> <p>And you can specialize templated functions or classes, to optimize specific cases or work with classes (or C <code>struct</code>s) that don't conform to the relevant interface:</p> <pre><code>/* * optimization: * primitive integers can be bit-shifted left to * achieve multiplication by powers of 2 * (note that any decent compiler will do this * for you, behind the hood.) */ template &lt;&gt; inline int doubleNum&lt;int&gt;(int n) { return n&lt;&lt;1; } /* * optimization: * use assembly code */ template &lt;&gt; inline int32_t doubleNum&lt;int32_t&gt;(int32_t n) { asm { ... } } /* * work with awkward number class */ template &lt;&gt; inline AwkwardNumber doubleNum&lt;AwkwardNumber&gt;(AwkwardNumber n) { n.multiplyBy(2); return n; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    2. 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