-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert-CTR55.cpp
50 lines (42 loc) · 1 KB
/
cert-CTR55.cpp
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
50
//
// CTR55-CPP. Do not use an additive operator on an iterator if the result would overflow
//
#include <algorithm>
#include <iostream>
#include <vector>
namespace {
size_t bad(const std::vector<int>& c)
{
constexpr size_t maxSize = 20;
size_t count = 0;
for (auto i = c.begin(), e = i + maxSize; i != e; ++i) {
std::cout << *i << ", "; // diagnostic required
++count;
}
std::cout << std::endl;
return count;
}
void good(const std::vector<int>& c)
{
constexpr std::vector<int>::size_type maxSize = 20;
for (auto i = c.begin(), e = i + std::min(maxSize, c.size()); i != e; ++i) {
std::cout << *i << ", ";
}
std::cout << std::endl;
}
void perfect(const std::vector<int>& c)
{
for (const auto& i : c) {
std::cout << i << ", ";
}
std::cout << std::endl;
}
} // namespace
int main()
{
std::vector<int> v{1, 2, 3, 4, 5, 6, 7};
size_t result = bad(v);
good(v);
perfect(v);
return static_cast<int>(result != v.size());
}