constexpr reference push_back(const T& x); // (1) C++26
constexpr reference push_back(T&& x); // (2) C++26
概要
新たな要素を末尾に追加する。
- (1) :
xをコピーして末尾に追加する。 - (2) :
xをムーブして末尾に追加する。
戻り値
追加された要素への参照。
例外
size() == Nの場合、std::bad_alloc例外を送出する。
計算量
定数時間
備考
std::vectorのpush_back()がvoidを返すのとは異なり、inplace_vectorでは追加された要素への参照を返す。
例
#include <print>
#include <inplace_vector>
#include <string>
int main()
{
std::inplace_vector<std::string, 5> iv;
// (1) コピーで追加
std::string s = "hello";
iv.push_back(s);
// (2) ムーブで追加
iv.push_back("world");
for (const auto& x : iv) {
std::println("{}", x);
}
}
出力
hello
world
バージョン
言語
- C++26
処理系
- Clang: 23 ✅
- GCC: 16 ✅
- Visual C++: 2026 Update 2 ❌