3 ms·
Obligatory shameless SaferCPlusPlus[1] plug: It is now practical to eliminate the use of unsafe C/C++ elements (like pointers and iterators) by replacing them w
by duneroadrunner 9y ago
Obligatory shameless SaferCPlusPlus[1] plug: It is now practical to eliminate the use of unsafe C/C++ elements (like pointers and iterators) by replacing them with safe, compatible substitutes. Using the SaferCPlusPlus library, your example code could become:
#include "msemstdvector.h"
// ...
auto v = mse::mstd::vector<int> { 1, 2, 3 };
auto e = v.begin() + 0; // 'e' is a safe iterator here
// ...
v.push_back(v.size());
// ...
cout << *e << "\n"; // no problem
Not much different from your original code.
Rust's policy of static enforcement of "exclusive mutable references" still might be preferable in many cases, but there is a practical option of using C++ in a safe(r) way.
[1] https://github.com/duneroadrunner/SaferCPlusPlus https://github.com/duneroadrunner/SaferCPlusPlus