如何在编译时将C字符串转换为int类型
How do I convert a C string to a int at compile time?
我希望能够传递整数或双精度(或字符串)作为模板参数和在某些情况下将结果转换为整数并将其用作类中类型的模板参数。
这是我尝试过的:
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::stoi
和atoi
,但也不是constexpr
…有什么想法可以做到这一点(我不能改变模板参数直接采取一个int
,因为它可能是一个双)。
用普通的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表达式是无效的,因此它会触发编译时错误,而不是实际抛出。
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);
}
如果您知道字符串的长度,您可以使用预处理器这样做:
#define BINARY_STOI(X) (X[0]-'0') + 2*(X[1]-'0')
+ 4*(X[2]-'0') + 8*(X[3]-'0')
+ 16*(X[4]-'0') + 32*(X[5]-'0')
+ 64*(X[6]-'0') + 128*(X[7]-'0')
#define DECIMAL_STOI(X) (X[0]-'0') + 10*(X[1]-'0')
+ 100*(X[2]-'0') + 1000*(X[3]-'0')
+ 10000*(X[4]-'0') + 100000*(X[5]-'0')
+ 1000000*(X[6]-'0') + 10000000*(X[7]-'0')
你只要继续这个模式不管你需要多少字符
相关文章:
- wcstombs_s(),转换字符串的长度
- 使用 CStringW/CStringA 和 CT2W/CT2A 转换字符串有什么区别?
- 无法转换字符串插入函数的参数
- 转换字符串向量:基于先前的值跳过元素
- 从中间器转换字符串不起作用,出了什么问题?
- 失败的证明转换字符串到枚举类
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 使用java jni时转换字符串类型
- 如何避免在转换字符串令牌流时重复istringstream构造
- 阿托伊未正确转换字符串
- C++,转换字符串,使连续下划线序列变为单个下划线
- 使用长算术转换字符串
- 转换字符串十进制数时的精度
- 将词法转换字符串提升为双精度
- 使用marshal_as函数转换字符串的性能
- C++不会从数据转换字符串
- 无论如何,在c++ /Qt中转换字符串到结构中的字段
- C++ 隐式类型转换字符串 -> int?
- 转换字符串到浮点数,c++实现
- 用于转换字符串大小写的函数