C++ 如何在编译时将C字符串转换为int?
我希望能够传递整数或双精度(或字符串)作为模板参数,并在某些情况下将结果转换为整数,并将其用作类中某个类型的模板参数 以下是我尝试过的:C++ 如何在编译时将C字符串转换为int?,c++,c++11,boost-mpl,C++,C++11,Boost Mpl,我希望能够传递整数或双精度(或字符串)作为模板参数,并在某些情况下将结果转换为整数,并将其用作类中某个类型的模板参数 以下是我尝试过的: 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
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;
};
模板
甲级
{
//下面的方法很好
int fun()
{
//此函数应返回传递给它的boost mpl类型中的int
//(例如,其形式可能为“123”)
返回std::stoi(boost::mpl::c_str::value);
}
//由于std::stoi不是constexpr,因此以下命令中断
std::阵列arr;
};
我可以这样做吗?我试过
std::stoi
和atoi
,但constepr
都没有。。。任何关于如何实现这一点的想法(我不能改变模板参数来直接获取int
,因为它可能是双精度的)。使用常规C字符串定义constepr 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, "...");
}
constexpr bool是数字(字符c){
返回c='0';
}
constexpr int stoi_impl(const char*str,int value=0){
返回*str?
是否为数字(*str)?
stoi_impl(str+1,(*str-'0')+值*10)
:抛出“编译时错误:不是数字”
:价值;
}
constexpr int stoi(const char*str){
返回stoi_impl(str);
}
int main(){
静态断言(stoi(“10”)==10,“…”);
}
抛出表达式在常量表达式中使用时无效,因此它将触发编译时错误,而不是实际抛出。mystoi():
你的标题是double,代码是integer。哪一个?一个比另一个难。查看代码,我甚至不知道您希望它如何工作。我记得我看到过
atoi
的constexpr实现,这个站点的某处std::array
有两个模板参数,typename T
和size\u T N
。你想要哪一个?因为double
将从size\u t
截断。是否只想检测字符串是否可以是int
或double
?在C++14中,这些constepr
函数变得非常简单。您几乎可以实现一个简单的atoi
并将其标记为constexpr
。看看这里:,作者定义了一个constexpratoi
。我还记得在SO上看到过一个类似的问题,但我再也找不到了。我在这里找到了编译时itoa
:无效抛出是如何工作的?编译该表达式时,即使为_digit(*str)
为true
,编译器是否也会导致错误?@ikh“常量表达式”中只允许某些内容。如果您在“常量表达式”上下文中使用constexpr
函数,并且出现了不允许的内容之一,编译器将无法编译它。@Rapptz噢,如果不存在,我们当然需要一点长的模板元编程>否则这不适用于INT\u MIN
。将设为负数
一个值为1
或-1
的整数,并像这样使用结果+=数字*乘数*为负数代码>@Chnossos:thumbsup:
#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;
}
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);
}