繁体   English   中英

升压::二进制<>

[英]Boost::binary<>

像二进制这样的boost库中有什么东西吗? 例如,我想写:

binary<10101> a;

我很惭愧地承认我已经试图找到它(Google,Boost),但没有结果。 他们提到了一些关于binary_int <>的内容,但我既不知道它是否可用,也不会找到我应该包含的头文件;

感谢帮助。

BOOST_BINARY宏。 像这样用过

int array[BOOST_BINARY(1010)];
  // equivalent to int array[012]; (decimal 10)

跟你的例子一起:

template<int N> struct binary { static int const value = N; };
binary<BOOST_BINARY(10101)> a;

一旦某些编译器支持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