简体   繁体   English

升压::二进制<>

[英]Boost::binary<>

Is there anything in boost libraries like binary? 像二进制这样的boost库中有什么东西吗? 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. 我很惭愧地承认我已经试图找到它(Google,Boost),但没有结果。 They're mention something about binary_int<> but I couldn't find neither if it is available nor what header file shall I include; 他们提到了一些关于binary_int <>的内容,但我既不知道它是否可用,也不会找到我应该包含的头文件;

Thanks for help. 感谢帮助。

There is the BOOST_BINARY macro. BOOST_BINARY宏。 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 一旦某些编译器支持C ++ 0x的用户定义文字,您就可以编写

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM