constexpr reference at(size_type n); // (1) C++26
constexpr const_reference at(size_type n) const; // (2) C++26
概要
境界チェック付きの要素アクセス。
- (1) : 非
constな文脈で任意の要素への参照を取得する。 - (2) :
constな文脈で任意の要素への読み取り専用参照を取得する。
戻り値
n番目の要素への参照を返す。
例外
n >= size()の場合、std::out_of_range例外を送出する。
計算量
定数時間
例
#include <print>
#include <inplace_vector>
#include <stdexcept>
int main()
{
std::inplace_vector<int, 5> iv = {3, 1, 4};
int& x = iv.at(2);
std::println("{}", x);
try {
iv.at(3);
}
catch (std::out_of_range& ex) {
std::println("out of range");
}
}
出力
4
out of range
バージョン
言語
- C++26
処理系
- Clang: 23 ✅
- GCC: 16 ✅
- Visual C++: 2026 Update 2 ❌