Search code examples
c++c++11boost-mpl

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


I want to be able to pass an integer or a double (or a string) as a template argument and in some instances convert the result to an integer and use it as a template argument for a type in the class.

Here's what I've tried:

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

Can I do this somehow? I've tried std::stoi and atoi but neither are constexpr... Any ideas how this could be done (I cannot change the template parameter to take an int directly, as it might be a double).


Solution

  • 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.