std::stoul not throwing std::out_of_range
std::stoul not throwing std::out_of_range
请考虑以下代码:
#include <iostream>
#include <cstring>
#include <cerrno>
int main() {
unsigned long num = strtoul("0xFFFFFFFFFFFF11223344556677889900AABBCCDDEEFF", NULL, 16);
std::cout << std::strerror(errno) << "n";
unsigned long num2 = std::stoul("0xFFFFFFFFFFFF11223344556677889900AABBCCDDEEFF");
std::stoul("hello world");
return 0;
}
这段代码应该从strerror打印一些"超出范围",然后根据文档抛出out_of_range异常。它永远不应该到达最后一条线。
在实践中,它不会抛出第二个 stoul 语句。我尝试过 GCC 4.8.5 和 MinGW 8.2.0,两者都未能抛出out_of_range异常,并且仅在最后一个 stoul 语句上提供了invalid_argument。
这是一个应该报告的错误,还是我遗漏了什么,这是预期的行为?
std::stoul
的默认基数为 10。
stoul
读取 0
,x
无效,因此忽略字符串的其余部分并返回数值 0
。
使用与 strtoul
中类似的语法:
unsigned long num2 = std::stoul("0xFFFFFFFFFFFF11223344556677889900AABBCCDDEEFF", nullptr, 16);
或者自动扣除数字基数:
unsigned long num2 = std::stoul("0xFFFFFFFFFFFF11223344556677889900AABBCCDDEEFF", nullptr, 0);
以上两个版本都会抛出。在线查看!
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 在 std::unordered_map 中插入新的键/值对会导致"out of range"异常
- std::remove, std::move(range) and moved-from elements
- 如何使用boost :: range :: apapters ::通过std :: unordered_set转换
- "Invalid iterator range"将initializer_list插入 std::vector 时
- 为什么 range-for 找不到 std::istream_iterator 的开始和结束重载?
- Range-for-loops 和 std::vector<bool>
- Std::shared_ptr不能使用range for