constexpr void swap(inplace_vector& x)
noexcept(N == 0 || (is_nothrow_swappable_v<T> &&
is_nothrow_move_constructible_v<T>)); // (1) C++26
概要
他のinplace_vectorオブジェクトとデータを入れ替える。
効果
*thisとxの要素を入れ替える。
例外
N == 0、もしくは要素型Tがnothrowでswap可能かつnothrowでムーブ構築可能な場合は、例外を投げない。
計算量
size()とx.size()の大きいほうに対して線形時間。
std::vectorのswap()が定数時間であるのとは異なり、inplace_vectorでは要素の交換が必要なため線形時間となる。
備考
swapにより全てのイテレータが無効化される。
例
#include <print>
#include <inplace_vector>
int main()
{
std::inplace_vector<int, 5> v1 = {1, 2, 3};
std::inplace_vector<int, 5> v2 = {4, 5};
v1.swap(v2);
std::print("v1:");
for (int x : v1) std::print(" {}", x);
std::println("");
std::print("v2:");
for (int x : v2) std::print(" {}", x);
std::println("");
}
出力
v1: 4 5
v2: 1 2 3
バージョン
言語
- C++26
処理系
- Clang: 23 ✅
- GCC: 16 ✅
- Visual C++: 2026 Update 2 ❌