C++ Logo


Advanced search

[std-proposals] std::at

From: blacktea hamburger <greenteahamburger_at_[hidden]>
Date: Sun, 16 Oct 2022 18:00:12 +0800
Basically, classes that provide operator[] provide at, such as std::vector:

std::vector<int> v{1, 2, 3};
v[3] = 0; // UB;
v.at(3) = 0; // throws std::out_of_range

But not for built-in arrays and classes that don't provide at.

So std::at (not std::ranges::at because it doesn't work with ranges) can be
added. For:

C c;
std::at(c, pos);

If C is a class that provides at, simply call it. Otherwise, If C does not
provide operator[] or C is built-in array, throw std::out_of_range if pos
>= std::ranges::size(C), if not, call c[pos]. Otherwise, it is ill-formed.

Received on 2022-10-16 10:00:43