A very strange behavior of this program, when I changed it (shown in
RED):
Code:
#include <iostream>
#include <vector>
template <typename T, typename F>
void forAll(T& array, F f) {
for (int i=0; i<array.size(); i++) {
f(array[i]);
}
}
template <typename T>
void plus2(T& v) {
v += 2;
}
int main() {
std::vector<int> vec;
vec.push_back(10);
vec.push_back(20);
vec.push_back(30);
vec.pop_back();
vec.pop_back();
vec.pop_back();
forAll(vec, plus2<int>);
std::cout << vec[0] << " " << vec[1] << " " << vec[2] << std::endl;
return 0;
}
Output:
10 20 30
And also, pop_back() does not return anything. Just decreases the size() of std::vector.