提升精神不检查 int 溢出
Boost spirit do not check for int overflow
我正在尝试使用boost spirit解析一些整数,我必须检查溢出的数据,spirit doc说所有整数解析器都会检查溢出,但它仅适用于无符号类型,如果我尝试解析有符号整数,spirit将不再检查溢出。
char const* b = "6600452345243524352340";
char const* e = b + strlen("6600452345243524352340");
int32_t res = 0;
bool valid = boost::spirit::qi::parse(b, e, boost::spirit::qi::int_, res);
std::cout << valid << " " << res << std::endl;
有没有办法对签名的溢出进行精神检查?我正在使用提升 1.55、gcc 4.9。
两者都可以很好地检查溢出:
住在科里鲁
#include <boost/spirit/include/qi.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::int128_t;
using boost::multiprecision::int256_t;
using boost::multiprecision::uint128_t;
using boost::multiprecision::uint256_t;
template <typename T>
bool try_parse(char const* s) {
auto b = s, e = b + strlen(s);
T res = 0;
namespace qi = boost::spirit::qi;
bool valid = qi::parse(b, e, qi::int_parser<T, 10>(), res);
if (!valid)
std::cout << "Unparsed (" << __PRETTY_FUNCTION__ << ")n";
else
std::cout << "Valid: " << res << "(" << __PRETTY_FUNCTION__ << ")n";;
if (b!=e)
std::cout << " --> remaining: '" << std::string(b,e) << "'n";
return valid;
}
int main() {
try_parse<int8_t> ("6600452345243524352340");
try_parse<uint8_t> ("6600452345243524352340");
try_parse<int16_t> ("6600452345243524352340");
try_parse<uint16_t> ("6600452345243524352340");
try_parse<int32_t> ("6600452345243524352340");
try_parse<uint32_t> ("6600452345243524352340");
try_parse<int64_t> ("6600452345243524352340");
try_parse<uint64_t> ("6600452345243524352340");
try_parse<int128_t> ("6600452345243524352340");
try_parse<uint128_t> ("6600452345243524352340");
}
指纹
Unparsed (bool try_parse(const char *) [T = signed char])
--> remaining: '6600452345243524352340'
Unparsed (bool try_parse(const char *) [T = unsigned char])
--> remaining: '6600452345243524352340'
Unparsed (bool try_parse(const char *) [T = short])
--> remaining: '6600452345243524352340'
Unparsed (bool try_parse(const char *) [T = unsigned short])
--> remaining: '6600452345243524352340'
Unparsed (bool try_parse(const char *) [T = int])
--> remaining: '6600452345243524352340'
Unparsed (bool try_parse(const char *) [T = unsigned int])
--> remaining: '6600452345243524352340'
Unparsed (bool try_parse(const char *) [T = long])
--> remaining: '6600452345243524352340'
Unparsed (bool try_parse(const char *) [T = unsigned long])
--> remaining: '6600452345243524352340'
Valid: 6600452345243524352340 (bool try_parse(const char *) [T = boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<128, 128, boost::multiprecision::cpp_integer_type::signed_magnitude, boost::multiprecision::cpp_int_check_type::unchecked, void>, boost::multiprecision::expression_template_option::et_off>])
Valid: 6600452345243524352340 (bool try_parse(const char *) [T = boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<128, 128, boost::multiprecision::cpp_integer_type::unsigned_magnitude, boost::multiprecision::cpp_int_check_type::unchecked, void>, boost::multiprecision::expression_template_option::et_off>])
相关文章:
- 'short int'持有的值溢出,但"自动"不会溢出?
- 我的 int main() 中出现堆栈溢出错误
- 运行时错误:有符号整数溢出:964632435 * 10 无法在类型 'int' 中表示
- 为什么 cin.get(char, int) 不会溢出?
- 创建具有未定义溢出的无符号 int
- 将 Vector<int> 推送到 Vector<vector 时出现堆溢出错误<int>>
- C++长 int 溢出/下溢
- 3 * 1000000000 溢出为 int,但变量很长.为什么
- 以下程序中最大 int 1000 000(在 int 范围内)的整数溢出
- 如何应对 int 溢出
- 提升精神不检查 int 溢出
- 将long-long与int进行比较时为什么会溢出
- 在涉及溢出错误的情况下,从int转换为char
- 无符号Int.溢出
- 从istream中读取int,检测溢出
- 检查c++中的int溢出
- 在溢出的情况下,i++是否对小于int的有符号类型调用未定义的行为?
- 每次我通过ifstream读取一个特定的输入时,输入就会改变,可能是int溢出
- 在objective-c中将c字符串解析为int以避免溢出
- 使用长int来避免溢出是一种好的做法吗