template <unsigned Max>
void foo(unsigned in_x)
{
unsigned cap = Max;
for (; cap != 0 && in_x != 0; cap >>= 1, in_x >>= 1)
{
}
}
As shown in the above code, I assume that if I just write
for (; in_x! = 0; in_x → = 1)
the compiler will not expand the loop because it cannot be sure of the maximum possible in_x.
I want to know if I am right or not, and if there are any more effective ways to deal with such things.
Or maybe the problem can be generalized as if some code could be written to tell the compiler a range of some runtime value, and such code does not have to be compiled into a binary runtime file.
Truly fighting the XD compiler
template <int Max>
__forceinline void find(int **in_a, int in_size, int in_key)
{
if (in_size == 0)
{
return;
}
if (Max == 0)
{
return;
}
{
int m = in_size / 2;
if ((*in_a)[m] >= in_key)
{
find<Max / 2>(in_a, m, in_key);
}
else
{
*in_a = *in_a + m + 1;
find<Max - Max / 2 - 1>(in_a, in_size - (m + 1), in_key);
}
}
}
source
share