Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, first you have to understand that trying to get a value out of an array can give you a pointer to its first element:</p> <pre><code>int a[] = {1, 2, 3}; int *ap = a; // a pointer, size is lost int (&amp;ar)[3] = a; // a reference to the array, size is not lost </code></pre> <p>References refer to objects using their exact type or their base-class type. The key is that the template takes arrays by reference. Arrays (not references to them) as parameters do not exist in C++. If you give a parameter an array type, it will be a pointer instead. So using a reference is necessary when we want to know the size of the passed array. The size and the element type are automatically deduced, as is generally the case for function templates. The following template</p> <pre><code>template&lt;typename T, size_t n&gt; size_t array_size(const T (&amp;)[n]) { return n; } </code></pre> <p>Called with our previously defined array <code>a</code> will implicitly instantiate the following function:</p> <pre><code>size_t array_size(const int (&amp;)[3]) { return 3; } </code></pre> <p>Which can be used like this:</p> <pre><code>size_t size_of_a = array_size(a); </code></pre> <hr> <p>There's a variation I made up some time ago <em>[Edit: turns out someone already had that same idea <a href="http://blogs.msdn.com/the1/archive/2004/05/07/128242.aspx" rel="noreferrer">here</a>]</em> which can determine a value at compile time. Instead of returning the value directly, it gives the template a return type depending on <code>n</code>:</p> <pre><code>template&lt;typename T, size_t n&gt; char (&amp; array_size(const T (&amp;)[n]) )[n]; </code></pre> <p>You say if the array has <code>n</code> elements, the return type is a reference to an array having size <code>n</code> and element type <code>char</code>. Now, you can get a compile-time determined size of the passed array:</p> <pre><code>size_t size_of_a = sizeof(array_size(a)); </code></pre> <p>Because an array of <code>char</code> having <code>n</code> elements has sizeof <code>n</code>, that will give you the number of elements in the given array too. At compile time, so you can do</p> <pre><code>int havingSameSize[sizeof(array_size(a))]; </code></pre> <p>Because the function never is actually called, it doesn't need to be defined, so it doesn't have a body. Hope I could clear the matter up a little bit.</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