簡體   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