1
- #include < vector>
2
1
#include < array>
2
+ #include < vector>
3
3
4
4
/* Helper functions */
5
5
std::vector<int > getData () { return {1 , 2 , 3 }; }
@@ -10,8 +10,8 @@ class MyContainer {
10
10
public:
11
11
MyContainer () = default ;
12
12
MyContainer (std::vector<int > data) : data_(data) {}
13
- std::vector<int >::const_iterator begin () const { return data_.begin (); }
14
- std::vector<int >::const_iterator end () const { return data_.end (); }
13
+ std::vector<int >::iterator begin () { return data_.begin (); }
14
+ std::vector<int >::iterator end () { return data_.end (); }
15
15
16
16
private:
17
17
std::vector<int > data_{7 , 8 , 9 };
@@ -20,15 +20,16 @@ class MyContainer {
20
20
class ConvertibleToVector {
21
21
public:
22
22
operator std::vector<int >() const { return {7 , 8 , 9 }; }
23
- std::array<int , 3 >::const_iterator begin () const { return data_.begin (); }
24
- std::array<int , 3 >::const_iterator end () const { return data_.end (); }
23
+ std::array<int , 3 >::iterator begin () { return data_.begin (); }
24
+ std::array<int , 3 >::iterator end () { return data_.end (); }
25
+ std::array<int , 3 >::const_iterator begin () const { return data_.cbegin (); }
26
+ std::array<int , 3 >::const_iterator end () const { return data_.cend (); }
25
27
26
28
private:
27
29
std::array<int , 3 > data_{7 , 8 , 9 };
28
30
};
29
31
30
- std::vector<int > operator +(std::vector<int > a,
31
- std::vector<int > b) {
32
+ std::vector<int > operator +(std::vector<int > a, std::vector<int > b) {
32
33
std::vector<int > result = a;
33
34
result.insert (result.end (), b.begin (), b.end ());
34
35
return result;
@@ -49,8 +50,12 @@ int main() {
49
50
50
51
/* ========== 2. OBJECT CREATION (CONSTRUCTOR CALLS) ========== */
51
52
53
+ for (auto x : std::vector<int >(3 )) { // COMPLIANT: 1 constructor call only
54
+ }
55
+
52
56
for (auto x :
53
- std::vector<int >{1 , 2 , 3 }) { // COMPLIANT: 1 constructor call only
57
+ std::vector<int >{1 , 2 , 3 }) { // NON_COMPLIANT: 2 constructor call to
58
+ // `vector` and `initializer_list`, respectively
54
59
}
55
60
56
61
for (auto x : MyContainer ()) { // COMPLIANT: 1 constructor call only
@@ -77,12 +82,22 @@ int main() {
77
82
78
83
/* ========== 3. OPERATOR OVERLOADING ========== */
79
84
85
+ std::vector<int > vec1 = {1 }, vec2 = {2 }, vec3 = {3 };
86
+ std::vector<int > appendedVector = (vec1 + vec2) + vec3;
87
+ for (auto x : appendedVector) { // COMPLIANT: 0 calls
88
+ }
89
+
90
+ std::vector<int > appendedVector2 = getData () + processData (localVec);
91
+ for (auto x : appendedVector2) { // COMPLIANT: 0 calls
92
+ }
93
+
80
94
std::vector<int > anotherVec = {4 , 5 , 6 };
81
- for (auto x : localVec + anotherVec) { // COMPLIANT: 1 operator+ call only
95
+ for (auto x : localVec + anotherVec) { // NON_COMPLIANT: 2 calls to vector's
96
+ // constructor, 1 operator+ call
82
97
}
83
98
84
- std::vector< int > vec1 = { 1 }, vec2 = { 2 }, vec3 = { 3 };
85
- for ( auto x : (vec1 + vec2) + vec3) { // NON-COMPLIANT: 2 operator+ calls
99
+ for ( auto x : ( vec1 + vec2) + vec3) { // NON-COMPLIANT: 3 calls to vector's
100
+ // constructor, 2 operator+ calls
86
101
}
87
102
88
103
for (auto x :
@@ -91,18 +106,11 @@ int main() {
91
106
localVec)) { // NON-COMPLIANT: 2 function calls + 1 operator call
92
107
}
93
108
94
- std::vector<int > appendedVector = (vec1 + vec2) + vec3;
95
- for (auto x : appendedVector) { // COMPLIANT: 0 calls
96
- }
97
-
98
- std::vector<int > appendedVector2 = getData () + processData (localVec);
99
- for (auto x : appendedVector2) { // COMPLIANT: 0 calls
100
- }
101
-
102
109
/* ========== 4. IMPLICIT CONVERSIONS ========== */
103
110
104
111
ConvertibleToVector convertible;
105
- for (int x : convertible) { // COMPLIANT: 1 conversion operator call only
112
+ for (int x :
113
+ ConvertibleToVector ()) { // COMPLIANT: 1 conversion operator call only
106
114
}
107
115
108
116
for (int x :
@@ -116,12 +124,10 @@ int main() {
116
124
}
117
125
118
126
std::vector<int > intVector1 = convertToIntVector (convertible);
119
- for (int x : intVector1) { // NON_COMPLIANT: 1 function call + 1
120
- // conversion operator call
127
+ for (int x : intVector1) { // COMPLIANT: 0 function calls
121
128
}
122
129
123
130
std::vector<int > intVector2 = convertToIntVector (convertible);
124
- for (int x : intVector2) { // NON_COMPLIANT: 1 function call + 1
125
- // conversion operator call
131
+ for (int x : intVector2) { // COMPLIANT: 0 function calls
126
132
}
127
133
}
0 commit comments