C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Draft 2: Error on out-of-bounds index

From: Bo Persson <bo_at_[hidden]>
Date: Sat, 30 Aug 2025 00:22:38 +0200
On 2025-08-29 at 23:43, Levo D via Std-Proposals wrote:
> The goal of my proposal is to have all bounds be checked at compile time. If I were including containers, the check would be for all containers (and arrays).
> I haven't seen "hardened preconditions," but glancing at P3471R0, it looks like it's a runtime check, which isn't my goal.

It doesn't have to be a runtime check. If the *caller* checks the
precondition before using vector's indexing, that could be optimized
out. For example in

for (size_t i {0}; i != vec.size(); ++i)
    vec[i] = 93;

it is pretty obvious that the precondition "pre (i < size())" is always
true.

Received on 2025-08-29 22:22:45