Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Non-type template arguments can also be pointers or references, provided they point or refer to an object with external linkage.</p> <pre><code>template&lt;typename T, T&amp; t&gt; struct ref { static T&amp; get() { return t; } }; int i = 0; int&amp; ri = ref&lt;int, i&gt;::get(); // ok static int j = 0; int&amp; rj = ref&lt;int, j&gt;::get(); // not ok const int jj = 0; // here, const implies internal linkage; whoops const int&amp; rjj = ref&lt;const int, jj&gt;::get(); // not ok extern const int k = 0; const int&amp; rk = ref&lt;const int, k&gt;::get(); // ok namespace { int l = 0; } int&amp; rl = ref&lt;int, l&gt;::get(); // ok, and l is specific to the TU </code></pre> <p>I don't think you'd really want to init the elements with extern references though, since that would end up with twice the number of objects. You <em>could</em> initialize the elements of the array from literals, but unfortunately you can't <a href="https://stackoverflow.com/questions/2033110/passing-a-string-literal-as-a-parameter-to-a-c-template-class">use string literals as template arguments</a>. <s>So you'd need the proverbial layer of indirection:</s> It's painful because arrays or array references can't appear in a template parameter list (I guess this is why string literals can't):</p> <pre><code>// Not possible: // const char* lits[] = { "Hello, ", "World!" }; // lit accepts const char*&amp;, not const char* // typedef array_&lt;T, lit&lt;lits[0]&gt;, lit&lt;lits[1]&gt;, int_&lt;42&gt; &gt; array; // instead, but painful: const char* hello = "Hello"; const char* world = "World!"; typedef array_&lt;T, lit&lt;hello&gt;, lit&lt;world&gt;, int_&lt;42&gt; &gt; array; /* * here array::data would be an array of T, size 3, * initialized from { hello, world, 42 } */ </code></pre> <p>I can't see how to avoid dynamic initialization without C++0x's <code>constexpr</code>, and even then there are limitations. Using some kind of tuple to build composite initializers (e.g. initialize from <code>{ { hello, world, 42 }, ... }</code>) left as an exercise. But <a href="http://ideone.com/knlFA" rel="nofollow noreferrer">here's an example</a>.</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.
    2. 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