C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Efficient and silent bounds checking with silent_at()

From: Bjorn Reese <breese_at_[hidden]>
Date: Thu, 6 Jul 2023 13:45:55 +0200
On 7/6/23 13:45, Bjorn Reese via Std-Proposals wrote:
> On 7/6/23 03:35, trtaab trtaab via Std-Proposals wrote:
>
>> By introducing the "silent_at" method, we provide an alternative
>> approach that allows for efficient bounds checking without the
>> overhead and potential performance implications of exceptions. The
>> "silent_at" method ensures controlled program termination through a
>> crash mechanism, such as __builtin_trap(), in the event of an
>> out-of-bounds access.
>
> You may consider using a free function instead
>
> template <typename T>
> auto silent_at(const T& container, size_type index) noe

noexcept

> {
> if (container.size() <= index)
> {
> // Your project-specific termination
> std::terminate();
> }
> return container[index];
> }
>

Received on 2023-07-06 11:45:57