【问题标题】:How do I convert a C string to a int at compile time?如何在编译时将 C 字符串转换为 int?
【发布时间】:2014-08-08 02:20:31
【问题描述】:

我希望能够将整数或双精度(或字符串)作为模板参数传递并且在某些情况下将结果转换为整数并将其用作模板参数输入类。

这是我尝试过的:

template <typename MPLString>
class A
{
    // the following works fine
    int fun()
    {
      // this function should return the int in the boost mpl type passed to it
      // (e.g. it might be of the form "123")
      return std::stoi(boost::mpl::c_str<MPLString>::value);
    }

    // the following breaks because std::stoi is not constexpr
    std::array<int, std::stoi(boost::mpl::c_str<MPLString>::value)> arr;
};

我能以某种方式做到这一点吗?我已经尝试过std::stoiatoi,但constexpr 都没有...任何想法如何做到这一点(我无法更改模板参数以直接采用int,如它可能是双倍的)。

【问题讨论】:

  • 你的标题是双精度,你的代码是整数。哪一个?一个比另一个更难。查看代码,我什至不确定您希望它如何工作。
  • 我记得我在某个地方看到过 atoi 的 constexpr 实现
  • std::array 有两个模板参数,typename Tsize_t N。你想要哪一个?因为double 只会从size_t 截断。您是否只想检测字符串是否可以是intdouble
  • 在 C++14 中,这些 constexpr 函数变得更加琐碎。您几乎可以实现一个幼稚的atoi 并将其标记为constexpr
  • 看看这里:enki-tech.blogspot.ca/2012/09/…,作者定义(除其他外)一个 constexpr atoi。我还记得在 SO 上看到过类似的问题,但再也找不到了。我在这里找到了编译时itoastackoverflow.com/q/6713420/3093378

标签: c++ c++11 boost-mpl


【解决方案1】:

使用常规 C 字符串定义 constexpr stoi 并不难。可以这样定义:

constexpr bool is_digit(char c) {
    return c <= '9' && c >= '0';
}

constexpr int stoi_impl(const char* str, int value = 0) {
    return *str ?
            is_digit(*str) ?
                stoi_impl(str + 1, (*str - '0') + value * 10)
                : throw "compile-time-error: not a digit"
            : value;
}

constexpr int stoi(const char* str) {
    return stoi_impl(str);
}

int main() {
    static_assert(stoi("10") == 10, "...");
}

throw 表达式在常量表达式中使用时无效,因此它会触发编译时错误而不是实际抛出。

【讨论】:

  • 无效抛出是如何工作的?编译该表达式时,即使is_digit(*str)true,编译器不会导致错误吗?
  • @ikh 在“常量表达式”中只允许某些内容。如果您在“常量表达式”上下文中使用constexpr 函数并且出现不允许的情况之一,编译器将无法编译它。
  • @Rapptz 哦,如果不存在,我们当然需要有点长的模板元编程>o
【解决方案2】:

mystoi():

#include <cstdint>     // for int32_t
#include <iosfwd>      // for ptrdiff_t, size_t
#include <iterator>    // for size
#include <stdexcept>   // for invalid_argument
#include <string_view> // for string_view

constexpr std::int32_t mystoi(std::string_view str, std::size_t* pos = nullptr) {
    using namespace std::literals;
    const auto numbers = "0123456789"sv;

    const auto begin = str.find_first_of(numbers);
    if (begin == std::string_view::npos)
        throw std::invalid_argument{"stoi"};

    const auto sign = begin != 0U && str[begin - 1U] == '-' ? -1 : 1;
    str.remove_prefix(begin);

    const auto end = str.find_first_not_of(numbers);
    if (end != std::string_view::npos)
        str.remove_suffix(std::size(str) - end);

    auto result = 0;
    auto multiplier = 1U;
    for (std::ptrdiff_t i = std::size(str) - 1U; i >= 0; --i) {
        auto number = str[i] - '0';
        result += number * multiplier * sign;
        multiplier *= 10U;
    }

    if (pos != nullptr) *pos = begin + std::size(str);
    return result;
}

main():

int main() {
    static_assert(mystoi(" 0   ") == 0);
    static_assert(mystoi(" 1   ") == 1);
    static_assert(mystoi("-1   ") == -1);
    static_assert(mystoi(" 12  ") == 12);
    static_assert(mystoi("-12  ") == -12);
    static_assert(mystoi(" 123 ") == 123);
    static_assert(mystoi("-123 ") == -123);
    static_assert(mystoi(" 1234") == 1234);
    static_assert(mystoi("-1234") == -1234);
    static_assert(mystoi("2147483647") == 2147483647);
    static_assert(mystoi("-2147483648") == -2147483648);
}

【讨论】:

  • 这不适用于INT_MIN。将 is_negative 设为具有值 1-1 的 int 并像这样使用它 result += number * multiplier * is_negative;
  • @Chnossos :thumbsup:
猜你喜欢
  • 2011-10-06
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多