L
Leo jay
i'd like to implement a class template to convert binary numbers to
decimal at compile time.
and my test cases are:
BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));
BOOST_STATIC_ASSERT((bin<1111>::value == 15));
BOOST_STATIC_ASSERT((bin<0>::value == 0));
BOOST_STATIC_ASSERT((bin<1010, 0011>::value == 163));
you can find my implementation at:
http://pastebin.org/2271
the first three cases were ok, but the last one failed, because,
compiler will parse 0011
as a octal number 9, instead of decimal number 11 because of the
leading 0s.
to resolve this, i defined 4 macros:
#define BIN1(a) bin<9##a>::value
#define BIN2(a, b) bin<9##a, 9##b>::value
#define BIN3(a, b, c) bin<9##a, 9##b, 9##c>::value
#define BIN4(a, b, c, d) bin<9##a, 9##b, 9##c, 9##d>::value
these macros could pass the last test case, but it's not good enough
that i have to specify how many numbers i will input.
is there any elegant way to resolve this?
thanks in advance.
decimal at compile time.
and my test cases are:
BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));
BOOST_STATIC_ASSERT((bin<1111>::value == 15));
BOOST_STATIC_ASSERT((bin<0>::value == 0));
BOOST_STATIC_ASSERT((bin<1010, 0011>::value == 163));
you can find my implementation at:
http://pastebin.org/2271
the first three cases were ok, but the last one failed, because,
compiler will parse 0011
as a octal number 9, instead of decimal number 11 because of the
leading 0s.
to resolve this, i defined 4 macros:
#define BIN1(a) bin<9##a>::value
#define BIN2(a, b) bin<9##a, 9##b>::value
#define BIN3(a, b, c) bin<9##a, 9##b, 9##c>::value
#define BIN4(a, b, c, d) bin<9##a, 9##b, 9##c, 9##d>::value
these macros could pass the last test case, but it's not good enough
that i have to specify how many numbers i will input.
is there any elegant way to resolve this?
thanks in advance.