使用constexpr将数字转换为字符串文字

syv*_*vex 9 c++ c++11

我正在寻找一种在编译时将数字转换为字符串文字的方法.它应该看起来像这样:

template <unsigned num>
struct num_to_string {
    constexpr static char value[] = /* ... magic goes here ... */;
};
Run Code Online (Sandbox Code Playgroud)

所以这num_to_string<5>::value等于"5"{'5', '\0'}.

这对于在编译时从一些其他constexpr数计算结果的数字生成字符串非常有用.

另请注意,我只对unsigned数字感兴趣,因为这应该更容易处理.签名版本的奖励积分:)

编辑:请注意,这类似于C++在编译时将整数转换为字符串,但不一样.在这里,我明确地想要使用constexpr而不是宏来帮助泛型编程.

tcl*_*amb 31

救援的变种模板.:)

namespace detail
{
    template<unsigned... digits>
    struct to_chars { static const char value[]; };

    template<unsigned... digits>
    constexpr char to_chars<digits...>::value[] = {('0' + digits)..., 0};

    template<unsigned rem, unsigned... digits>
    struct explode : explode<rem / 10, rem % 10, digits...> {};

    template<unsigned... digits>
    struct explode<0, digits...> : to_chars<digits...> {};
}

template<unsigned num>
struct num_to_string : detail::explode<num> {};
Run Code Online (Sandbox Code Playgroud)

与往常一样,这是Coliru的实例,显示了使用情况和(相关)生成的程序集.


同样适应这种方法来支持负数也很简单.这是一个更通用的表单,要求用户输入整数的类型:

namespace detail
{
    template<uint8_t... digits> struct positive_to_chars { static const char value[]; };
    template<uint8_t... digits> constexpr char positive_to_chars<digits...>::value[] = {('0' + digits)..., 0};

    template<uint8_t... digits> struct negative_to_chars { static const char value[]; };
    template<uint8_t... digits> constexpr char negative_to_chars<digits...>::value[] = {'-', ('0' + digits)..., 0};

    template<bool neg, uint8_t... digits>
    struct to_chars : positive_to_chars<digits...> {};

    template<uint8_t... digits>
    struct to_chars<true, digits...> : negative_to_chars<digits...> {};

    template<bool neg, uintmax_t rem, uint8_t... digits>
    struct explode : explode<neg, rem / 10, rem % 10, digits...> {};

    template<bool neg, uint8_t... digits>
    struct explode<neg, 0, digits...> : to_chars<neg, digits...> {};

    template<typename T>
    constexpr uintmax_t cabs(T num) { return (num < 0) ? -num : num; }
}

template<typename Integer, Integer num>
struct string_from : detail::explode<(num < 0), detail::cabs(num)> {};
Run Code Online (Sandbox Code Playgroud)

它的用法如下:

string_from<signed, -1>::value
Run Code Online (Sandbox Code Playgroud)

正如Coliru的实例中所示.


归档时间:

查看次数:

4905 次

最近记录:

8 年,3 月 前