How do I convert a C string to a int at compile time?

Defining a constexpr stoi isn't too hard with regular C strings. It can be defined as follows:

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, "...");
}

The throw expression is invalid when used in constant expressions so it'll trigger a compile time error rather than actually throwing.


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);
}