[英]Converting decimal to hexadecimal at compile time
I'm trying to create a program that can do conversions from a decimal number to a hexidecimal number at compile time. 我正在尝试创建一个程序,该程序可以在编译时从十进制数转换为十六进制数。 Unfortunately I'm new to metaprogramming and I can't figure out a way to do this at compile time.
不幸的是,我是元编程的新手,在编译时我找不到一种方法。 Any ideas how I can approach this?
有什么想法可以解决这个问题吗?
How can I represent such a number without violating the principles of template metaprogramming. 如何在不违反模板元编程原理的情况下表示这样的数字。
#include <algorithm> // std::reverse
#include <iterator> // std::begin, std::end
#include <iostream>
#include <tuple>
#include <type_traits> // std::integral_constant
namespace detail {
using std::tuple;
using std::tuple_cat;
using std::integral_constant;
template< unsigned x, int n_digits >
struct Hex_tuple_
{
using Type = decltype( tuple_cat(
typename Hex_tuple_< x & ((1 << 2*n_digits) - 1), n_digits/2 >::Type(),
typename Hex_tuple_< (x >> 2*n_digits), n_digits/2 >::Type()
) );
};
template< unsigned x >
struct Hex_tuple_< x, 1 >
{
using Type = tuple< integral_constant< int, x > >;
};
} // namespace detail
template< unsigned x >
using Hex_ = typename detail::Hex_tuple_< x, 2*sizeof( x ) >::Type;
template< int... digit >
auto operator<<(
std::ostream& stream,
const std::tuple< std::integral_constant< int, digit >... >&
)
-> std::ostream&
{
int digits[] = { digit... };
std::reverse( std::begin( digits ), std::end( digits ) );
for( int x : digits )
{
stream << "0123456789ABCDEF"[x];
}
return stream;
}
auto main() -> int
{
std::cout << Hex_<123456>() << "\n";
}
The general problem of constructing the base B
numeral that represents a given unsigned integer at compiletime is quite interesting. 构建在编译时表示给定无符号整数的基数
B
的一般问题非常有趣。 It can be solved concisely by an application of the valuable technique of building a compiletime std::array
, which goes back at least to this answer 可以通过应用构建编译时
std::array
的宝贵技术来简洁地解决此问题 ,至少可以回溯到此答案
The function template digits
as defined below returns a compiletime null-terminated std::array
of char
that contains the base Base
numeral representing the integer N
, for Base
in the reasonable range [2,36] (digits '0-9A-Z') 如下定义的函数模板
digits
将返回一个以char
表示的编译时以空值终止的std::array
,其中char
包含表示整数N
的基本Base
数字,对于在合理范围[2,36]中的Base
(数字'0-9A-Z' )
#include <type_traits>
#include <array>
// Get the number of base `base` digits in `n`
constexpr std::size_t base_digits(unsigned long n, unsigned base)
{
return (n / base) == 0 ? 1 : 1 + base_digits(n / base,base);
}
// Convert the remainder `r` of division by some base to a based digit
constexpr char based_digit(unsigned r)
{
return r > 36 ? '!' : r < 10 ? '0' + r : 'A' + r - 10;
}
template <unsigned long N, unsigned Base,
std::size_t Len = base_digits(N,Base) + 1, unsigned ...Rems>
constexpr std::array<char, Len>
digits(std::enable_if_t<(Base > 1 && Base <= 36 && N < Base)> * = nullptr)
{
return std::array<char,Len>{{based_digit(N), Rems...,0}};
}
template <unsigned long N, unsigned Base,
std::size_t Len = base_digits(N,Base) + 1, unsigned ...Rems>
constexpr std::array<char,Len>
digits(std::enable_if_t<(Base > 1 && Base <= 36 && N >= Base)> * = nullptr)
{
return digits<N / Base, Base, Len, based_digit(N % Base), Rems...>();
}
Prepend that to this test: 在此测试之前添加:
constexpr auto decimal = digits<9876543210,10>();
constexpr auto hex = digits<9876543210,16>();
constexpr auto octal = digits<123456789,8>();
constexpr auto binary = digits<123456789,2>();
constexpr auto b_36 = digits<36 + 35,36>();
static_assert(decimal.size() == 11,"");
#include <iostream>
#include <cassert>
int main() {
char * pend;
assert(std::strtoul(decimal.data(),&pend,10) == 9876543210);
std::cout << decimal.data() << std::endl;
assert(std::strtoul(hex.data(),&pend,16) == 9876543210);
std::cout << hex.data() << std::endl;
assert(std::strtoul(octal.data(),&pend,8) == 123456789);
std::cout << octal.data() << std::endl;
assert(std::strtoul(binary.data(),&pend,2) == 123456789);
std::cout << binary.data() << std::endl;
assert(std::strtoul(b_36.data(),&pend,36) == 36 + 35);
std::cout << b_36.data() << std::endl;
return 0;
}
Output is: 输出为:
9876543210
24CB016EA
726746425
111010110111100110100010101
1Z
(gcc 4.9.2/clang 3.5.1, -std=c++14. Trivially adaptable for C++11) (gcc 4.9.2 / clang 3.5.1,-std = c ++ 14。适用于C ++ 11)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.