boost库中有像二进制这样的东西吗?例如,我想这样写:
binary<10101> a;我很羞愧地承认我试着去找它(Google,Boost),但是没有结果。他们提到了一些关于binary_int<>的东西,但我既找不到它是否可用,也找不到我应该包括什么头文件;
谢谢你的帮助。
发布于 2010-03-20 22:32:14
这里有一个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];https://stackoverflow.com/questions/2483378
复制相似问题