Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is technically possible, it's just very very ugly. Here's a sample that generates a string literal for an unsigned int. It does not (yet) create a string of the form "1 2 3 ... i-1", however I'm sure it is possible if you're willing to spend the effort.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;limits&gt; /////////////////////////////////////////////////////////////////////////////// // exponentiation calculations template &lt;int accum, int base, int exp&gt; struct POWER_CORE : POWER_CORE&lt;accum * base, base, exp - 1&gt;{}; template &lt;int accum, int base&gt; struct POWER_CORE&lt;accum, base, 0&gt; { enum : int { val = accum }; }; template &lt;int base, int exp&gt; struct POWER : POWER_CORE&lt;1, base, exp&gt;{}; /////////////////////////////////////////////////////////////////////////////// // # of digit calculations template &lt;int depth, unsigned int i&gt; struct NUM_DIGITS_CORE : NUM_DIGITS_CORE&lt;depth + 1, i / 10&gt;{}; template &lt;int depth&gt; struct NUM_DIGITS_CORE&lt;depth, 0&gt; { enum : int { val = depth}; }; template &lt;int i&gt; struct NUM_DIGITS : NUM_DIGITS_CORE&lt;0, i&gt;{}; template &lt;&gt; struct NUM_DIGITS&lt;0&gt; { enum : int { val = 1 }; }; /////////////////////////////////////////////////////////////////////////////// // Convert digit to character (1 -&gt; '1') template &lt;int i&gt; struct DIGIT_TO_CHAR { enum : char{ val = i + 48 }; }; /////////////////////////////////////////////////////////////////////////////// // Find the digit at a given offset into a number of the form 0000000017 template &lt;unsigned int i, int place&gt; // place -&gt; [0 .. 10] struct DIGIT_AT { enum : char{ val = (i / POWER&lt;10, place&gt;::val) % 10 }; }; struct NULL_CHAR { enum : char{ val = '\0' }; }; /////////////////////////////////////////////////////////////////////////////// // Convert the digit at a given offset into a number of the form '0000000017' to a character template &lt;unsigned int i, int place&gt; // place -&gt; [0 .. 9] struct ALT_CHAR : DIGIT_TO_CHAR&lt; DIGIT_AT&lt;i, place&gt;::val &gt;{}; /////////////////////////////////////////////////////////////////////////////// // Convert the digit at a given offset into a number of the form '17' to a character // Template description, with specialization to generate null characters for out of range offsets template &lt;unsigned int i, int offset, int numDigits, bool inRange&gt; struct OFFSET_CHAR_CORE_CHECKED{}; template &lt;unsigned int i, int offset, int numDigits&gt; struct OFFSET_CHAR_CORE_CHECKED&lt;i, offset, numDigits, false&gt; : NULL_CHAR{}; template &lt;unsigned int i, int offset, int numDigits&gt; struct OFFSET_CHAR_CORE_CHECKED&lt;i, offset, numDigits, true&gt; : ALT_CHAR&lt;i, (numDigits - offset) - 1 &gt;{}; // Perform the range check and pass it on template &lt;unsigned int i, int offset, int numDigits&gt; struct OFFSET_CHAR_CORE : OFFSET_CHAR_CORE_CHECKED&lt;i, offset, numDigits, offset &lt; numDigits&gt;{}; // Calc the number of digits and pass it on template &lt;unsigned int i, int offset&gt; struct OFFSET_CHAR : OFFSET_CHAR_CORE&lt;i, offset, NUM_DIGITS&lt;i&gt;::val&gt;{}; /////////////////////////////////////////////////////////////////////////////// // Integer to char* template. Works on unsigned ints. template &lt;unsigned int i&gt; struct IntToStr { const static char str[]; }; template &lt;unsigned int i&gt; const char IntToStr&lt;i&gt;::str[] = { OFFSET_CHAR&lt;i, 0&gt;::val, OFFSET_CHAR&lt;i, 1&gt;::val, OFFSET_CHAR&lt;i, 2&gt;::val, OFFSET_CHAR&lt;i, 3&gt;::val, OFFSET_CHAR&lt;i, 4&gt;::val, OFFSET_CHAR&lt;i, 5&gt;::val, OFFSET_CHAR&lt;i, 6&gt;::val, OFFSET_CHAR&lt;i, 7&gt;::val, OFFSET_CHAR&lt;i, 8&gt;::val, OFFSET_CHAR&lt;i, 9&gt;::val, NULL_CHAR::val }; /////////////////////////////////////////////////////////////////////////////// // Tests int _tmain(int argc, _TCHAR* argv[]) { std::wcout &lt;&lt; IntToStr&lt;17&gt;::str &lt;&lt; std::endl; std::wcout &lt;&lt; IntToStr&lt;173457&gt;::str &lt;&lt; std::endl; std::wcout &lt;&lt; IntToStr&lt; INT_MAX &gt;::str &lt;&lt; std::endl; std::wcout &lt;&lt; IntToStr&lt;0&gt;::str &lt;&lt; std::endl; std::wcout &lt;&lt; IntToStr&lt;1&gt;::str &lt;&lt; std::endl; std::wcout &lt;&lt; IntToStr&lt;-1&gt;::str &lt;&lt; std::endl; return 0; } </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.
    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