3 ms·
> This is problematic when the default `[]` API that everyone uses has no bounds check. The default [] API can be replaced with C++ classes that do bounds chec
by rerdavies 3y ago
> This is problematic when the default `[]` API that everyone uses has no bounds check.
The default [] API can be replaced with C++ classes that do bounds checks. C++ 20 provides the std::array class to do precisely that, and std::span to implement fat pointers.
All that's missing to implement the subset-of-a-superset mode is a compiler option to disable native arrays in C++ code (but not in extern "C" code).
- vlovich123 3y ago> The default [] API can be replaced with C++ classes that do bounds checks Which means you subtly break the performance guarantees of code which makes migration to a new version more annoying. > C++ 20 provides the std::array class to do precisely that What? https://en.cppreference.com/w/cpp/container/array/operator_at https://en.cppreference.com/w/cpp/container/array/operator_a... > Returns a reference to the element at specified location pos. No bounds checking is performed > option to disable native arrays in C++ code Yeah, no that's not the only place where bounds checking shows up. Lots of places use pointers as iterators because the language lets you. So even if you shut off those avenues, code that uses pointers as iterators would remain exploitable. Of course it's a step improvement, but there's just no way to close the barn door for C++ unless you sacrifice performance to such a degree that the obvious question becomes "why restricted C/C++" which still has a bunch of footguns, is slow, and which has a really inconsistent API and language surface?