Skip to content

Instantly share code, notes, and snippets.

@jl2
Created June 20, 2013 17:39
Show Gist options
  • Select an option

  • Save jl2/5824873 to your computer and use it in GitHub Desktop.

Select an option

Save jl2/5824873 to your computer and use it in GitHub Desktop.
A stupid C++ bug. The insert() on line 23 has its last two parameters swapped, but "false" is silently converted to 0, so no compiler warnings/errors are raised.
#include <iostream>
#include <vector>
void Show(const std::vector<std::vector<bool>> &two_dim_vect) {
for (const auto &row : two_dim_vect) {
for (bool b : row) {
std::cout << b << " ";
}
std::cout << "\n";
}
}
int main() {
std::vector<std::vector<bool>> two_dim_vect;
std::vector<bool> tmp;
const int size1 = 5;
const int size2 = 5;
tmp.reserve(size1);
// This compiles without errors or warnings
// The last two parameters are swapped and "false" is treated as integer 0
tmp.insert(tmp.begin(), false, size1);
two_dim_vect.reserve(size2);
two_dim_vect.insert(two_dim_vect.begin(), size2, tmp);
Show(two_dim_vect);
two_dim_vect[3][2] = true;
Show(two_dim_vect);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment