This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <boost/static_assert.hpp> | |
template <unsigned long N> | |
struct binary | |
{ | |
BOOST_STATIC_ASSERT(N % 10 < 2); | |
static unsigned const value | |
= binary<N/10>::value << 1 // prepend higher bits | |
| N%10; // to lowest bit | |
}; | |
template <> // specialization | |
struct binary<0> // terminates recursion | |
{ | |
static unsigned const value = 0; | |
}; | |
unsigned const one = binary<1>::value; | |
unsigned const three = binary<11>::value; | |
unsigned const two = binary<10>::value; | |
unsigned const five = binary<101>::value; | |
unsigned const seven = binary<151>::value; | |
unsigned const nine = binary<1001>::value; | |
int main() | |
{ | |
std::cout << two << std::endl; | |
return 0; | |
} |