Created
June 20, 2013 17:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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