شما اول باید کدت رو به شکل بازگشتی بنویسی که میشه این :
unsigned binary(unsigned long N)
{
return N == 0 ? 0 : 2 * binary(N/10) + N%10 ;
}
بعد از این که کد رو نوشتی حالا راحت می تونی با Template زمان کامپایل این کار رو انجام بدی :
#include <iostream>
template <unsigned long N>
struct Binary
{
enum{value = Binary<N/10>::value *2 + N%10 };
};
template <>
struct Binary<0> //end
{
enum { value=0};
};
int main()
{
const unsigned number=Binary<1101>::value;
std::cout<<number;
return 0;
}