This is a motivating example:
int main()
{
const unsigned int di = 12;
const unsigned int oi = 014;
const unsigned int hi = 0xc;
const unsigned int bi0 = binary_value("1101");
const unsigned int bi1 = binary<1100>::value;
}
Why in compilation time?
Speed.
Constantness (array size, enum value must be known in compilation time.
Compiler control.
template <unsigned long N>
struct binary
{
static unsigned const value
= binary<N/10>::value * 2 // prepend higher bits
+ N%10; // to lowest bit
};
template <> // specialization
struct binary<0> // terminates recursion
{
static unsigned const value = 0;
};