4 ms·
> C++ has all the bits needed to check array bounds by default but chooses not to do so... std::array::at does bound check.
by riku_iki 2y ago
> C++ has all the bits needed to check array bounds by default but chooses not to do so...
std::array::at does bound check.
- grumpyprole 2y agoYes, but they've made the convenient lightweight syntax not bounds check. Defaults matter in languages. Other languages make you use an esoteric function e.g at_unsafe to skip bounds checking.
- riku_iki 2y agoits probably because per contract std::array has to behave exactly like C array for legacy ops. Also this is not zero cost abstraction, so programmer has a choice: ultimate performance or extra safety.
- pjmlp 2y agoWhile a bummer, most C++ compilers do have a build flag to enable bounds check in operator[](). Which most sane compilers will do for you in debug builds.
- retrac 2y ago* = [] and friends can be overloaded in C++. So just about any kind of data structure can masquerade as items[3] or *value. Opinions differ on whether this is the great strength or fatal flaw in C++.
- colonwqbang 2y agoAdding a special function for safe indexing doesn't really count, we can do that in C too. "By default" means the most natural and common way to index an array, namely the [] syntax.
- riku_iki 2y ago> "By default" means the most natural and common way to index an array, namely the [] syntax. Sorry, I challenge your authority to decide what is "most natural way".