Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction checking if an integer type can fit a value of possibly different (integer) type
    primarykey
    data
    text
    <p>Is it possible to create a templated function that checks if a primitive data type can fit a value of potentially different primitive data type? Let's limit the scope to integer types for the moment.</p> <p>More precisely: Is it possible to create a "one fit all" templated functions yet without getting compiler warnings (boolean expression always true/false, signed/unsigned comparison, unused variable) and without disabling compiler warning checks? The functions should also limit as much as possible checks at runtime (all trivial cases should be excluded at compile time). If possible, I would prefer avoiding using extensions from C++11 and the like (unless a "quick" replacement for "old" C++ exists).</p> <p>Note: "value" is not known at compile time, only its type.</p> <p>Example of expected behaviour:</p> <pre><code>int main(int argc, char** argv) { for (int i = 1; i &lt; argc; i++) { const int value = atoi(argv[i]); std::cout &lt;&lt; value &lt;&lt; ": "; std::cout &lt;&lt; CanTypeFitValue&lt;int8_t&gt;(value) &lt;&lt; " "; std::cout &lt;&lt; CanTypeFitValue&lt;uint8_t&gt;(value) &lt;&lt; " "; std::cout &lt;&lt; CanTypeFitValue&lt;int16_t&gt;(value) &lt;&lt; " "; std::cout &lt;&lt; CanTypeFitValue&lt;uint16_t&gt;(value) &lt;&lt; " "; std::cout &lt;&lt; CanTypeFitValue&lt;int32_t&gt;(value) &lt;&lt; " "; std::cout &lt;&lt; CanTypeFitValue&lt;uint32_t&gt;(value) &lt;&lt; " "; std::cout &lt;&lt; CanTypeFitValue&lt;int64_t&gt;(value) &lt;&lt; " "; std::cout &lt;&lt; CanTypeFitValue&lt;uint64_t&gt;(value) &lt;&lt; std::endl; } } ./a.out 6 1203032847 2394857 -13423 9324 -192992929 6: 1 1 1 1 1 1 1 1 1203032847: 0 0 0 0 1 1 1 1 2394857: 0 0 0 0 1 1 1 1 -13423: 0 0 1 0 1 0 1 0 9324: 0 0 1 1 1 1 1 1 -192992929: 0 0 0 0 1 0 1 0 </code></pre> <p>Test your code <a href="http://ideone.com/C5iael" rel="nofollow noreferrer">here</a> or <a href="http://coliru.stacked-crooked.com/view?id=563dadd8bd231acdfa7898188bbe33c8-ad7854d9cfd7979d567ca413f0830b65" rel="nofollow noreferrer">here</a>.</p> <p>Check the assembly generated <a href="http://gcc.godbolt.org/#%7B%22version%22%3A3%2C%22filterAsm%22%3A%7B%22labels%22%3Atrue%2C%22directives%22%3Atrue%2C%22commentOnly%22%3Atrue%7D%2C%22compilers%22%3A%5B%7B%22source%22%3A%22template%20%3Ctypename%20T%2C%20typename%20U%3E%5Cn%20%20%20%20bool%20CanTypeRepresentValue(const%20U%20value)%20%7B%5Cn%20%20%20%20%20%20%20%20return%20false%3B%5Cn%20%20%20%20%7D%5Cn%5Cn%5Cnbool%20test(int%20x)%20%7B%5Cn%20%20return%20CanTypeRepresentValue%3Cchar%3E(x)%3B%5Cn%7D%5Cn%5Cn%5Cn%22%2C%22compiler%22%3A%22%2Fusr%2Fbin%2Fg%2B%2B-4.8%22%2C%22options%22%3A%22-std%3Dc%2B%2B11%20-O2%20-fomit-frame-pointer%20-march%3Dnative%22%7D%5D%7D" rel="nofollow noreferrer">here</a>.</p> <p>This question was inspired by <a href="https://stackoverflow.com/q/17205700/2436175">this post</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    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