Boost::binary<>
Is there anything in boost l开发者_JAVA百科ibraries like binary? For example I would like to write:
binary<10101> a;
I'm ashamed to admit that I've tried to find it (Google, Boost) but no results. They're mention something about binary_int<> but I couldn't find neither if it is available nor what header file shall I include;
Thanks for help.
There is the BOOST_BINARY
macro. used like this
int array[BOOST_BINARY(1010)];
// equivalent to int array[012]; (decimal 10)
To go with your example:
template<int N> struct binary { static int const value = N; };
binary<BOOST_BINARY(10101)> a;
Once some compiler supports C++0x's user defined literals, you could write
template<char... digits>
struct conv2bin;
template<char high, char... digits>
struct conv2bin<high, digits...> {
static_assert(high == '0' || high == '1', "no bin num!");
static int const value = (high - '0') * (1 << sizeof...(digits)) +
conv2bin<digits...>::value;
};
template<char high>
struct conv2bin<high> {
static_assert(high == '0' || high == '1', "no bin num!");
static int const value = (high - '0');
};
template<char... digits>
constexpr int operator "" _b() {
return conv2bin<digits...>::value;
}
int array[1010_b];
精彩评论