Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not possible. The usual way you do it is this:</p> <pre><code>template&lt;int N&gt; struct foo { static const int value = N; }; </code></pre> <p>and for types</p> <pre><code>template&lt;typename T&gt; struct foo { typedef T type; }; </code></pre> <p>You can access it then as <code>foo&lt;39&gt;::value</code> or <code>foo&lt;int&gt;::type</code>. </p> <p>If you have a particular type, you can use partial template specialization:</p> <pre><code>template&lt;typename&gt; struct steal_it; template&lt;std::size_t N&gt; struct steal_it&lt; std::bitset&lt;N&gt; &gt; { static const std::size_t value = N; }; </code></pre> <p>The same principle is possible for type parameters too, indeed. Now you can pass any bitset to it, like <code>steal_it&lt; std::bitset&lt;16&gt; &gt;::value</code> (note to use size_t, not int!). Because we have no variadic many template paramters yet, we have to limit ourself to a particular parameter count, and repeat the steal_it template specializations for count from 1 up to N. Another difficulty is to scan types that have mixed parameters (types and non-types parameters). This is probably nontrivial to solve. </p> <p>If you have not the type, but only an object of it, you can use a trick, to still get a value at compile time:</p> <pre><code>template&lt;typename T&gt; char (&amp; getN(T const &amp;) )[steal_it&lt;T&gt;::value]; int main() { std::bitset&lt;16&gt; b; sizeof getN(b); // assuming you don't know the type, you can use the object } </code></pre> <p>The trick is to make the function template auto-deduce the type, and then return a reference to a character array. The function doesn't need to be defined, the only thing needed is its 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