首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Boost::binary<>

Boost::binary<>
EN

Stack Overflow用户
提问于 2010-03-20 22:26:58
回答 1查看 3.1K关注 0票数 2

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

代码语言:javascript
复制
binary<10101> a;

我很羞愧地承认我试着去找它(Google,Boost),但是没有结果。他们提到了一些关于binary_int<>的东西,但我既找不到它是否可用,也找不到我应该包括什么头文件;

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-03-20 22:32:14

这里有一个BOOST_BINARY宏。像这样使用

代码语言:javascript
复制
int array[BOOST_BINARY(1010)];
  // equivalent to int array[012]; (decimal 10)

为了配合您的示例:

代码语言:javascript
复制
template<int N> struct binary { static int const value = N; };
binary<BOOST_BINARY(10101)> a;

一旦一些编译器支持C++0x的用户定义的文字,您就可以编写

代码语言:javascript
复制
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];
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2483378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档