tags:

views:

139

answers:

1
+2  Q: 

Boost::binary<>

Hi, Is there anything in boost libraries 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.

+10  A: 

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];
Johannes Schaub - litb
I just looked at the source - I think I'll stick with hex :-)
anon
@Neil so how long did it take to get the "legendary" badge? I'm trying hard, but it seems difficult to get it :)
Johannes Schaub - litb
@Johannes Oh, it's easy - you simply need to have nothing better to do with your time. It's silly to be proud of any badge of course, but I am kind of proud of the the silver "stl" badge, because of the select bunch of people it has been awarded to :-)
anon
@Neil, ah i see. I'm really glad tho that herb sutter joined stackoverflow today: http://stackoverflow.com/users/297582/herb-sutter . Now C++ has a real-world expert and committee member on SO too.
Johannes Schaub - litb
@Johannes That is good news, but I wonder how long he'll last? Other well known figures such as the ghastly "Uncle Bob" Martin have shown up here and then vanished again. I wonder how long Herb can put up with being repeatedly asked what the meaning of ++++x is.
anon