-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA18-0-2.cpp
More file actions
49 lines (37 loc) · 966 Bytes
/
A18-0-2.cpp
File metadata and controls
49 lines (37 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Rule: A18-0-2
// Source line: 28442
// Original file: A18-0-2.cpp
// $Id: A18-0-2.cpp 312092 2018-03-16 15:47:01Z jan.babst $
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <string>
std::int32_t F1(const char* str) noexcept
{
return atoi(str); // Non-compliant - undefined behavior if str can not
// be converted
}
std::int32_t F2(std::string const& str) noexcept(false)
{
return std::stoi(str); // Compliant - throws a std::invalid_argument
// exception if str can not be converted
}
std::uint16_t ReadFromStdin1() // non-compliant
{
std::uint16_t a;
std::cin >> a; // no error detection
return a;
}
std::uint16_t ReadFromStdin2()
{
std::uint16_t a;
// compliant
std::cin.clear(); // clear all flags
std::cin >> a;
if (std::cin.fail())
{
throw std::runtime_error{"unable to read an integer"};
}
std::cin.clear(); // clear all flags for subsequent operations
return a;
}