Skip to content

Latest commit

 

History

History
70 lines (50 loc) · 1.83 KB

File metadata and controls

70 lines (50 loc) · 1.83 KB

swap

  • memory[meta header]
  • std[meta namespace]
  • indirect[meta class]
  • function[meta id-type]
  • cpp26[meta cpp]
constexpr void swap(indirect& other) noexcept(see below);

概要

*thisotherの状態を交換する。所有オブジェクトまたは無効値状態を交換する。

事前条件

allocator_traits<Allocator>::propagate_on_container_swap::valuetrueの場合、AllocatorはCpp17Swappable要件を満たすこと。そうでない場合、get_allocator() == other.get_allocator()trueであること。

効果

*thisotherの状態(所有オブジェクトまたは無効値状態)を交換する。propagate_on_container_swap::valuetrueの場合はアロケータも交換する。所有オブジェクトに対して直接swapを呼ぶわけではない。

例外

以下と等価なnoexcept指定を持つ:

noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value ||
         allocator_traits<Allocator>::is_always_equal::value)
  • allocator_traits[link /reference/memory/allocator_traits.md]

#include <cassert>
#include <memory>

int main()
{
  std::indirect<int> a{1};
  std::indirect<int> b{2};
  a.swap(b);
  assert(*a == 2 && *b == 1);
}
  • std::indirect[link ../indirect.md]
  • swap[color ff0000]

出力

バージョン

言語

  • C++26

処理系

  • Clang: 22 [mark noimpl]
  • GCC: 16.1 [mark verified]
  • Visual C++: 2026 Update 2 [mark noimpl]

関連項目

参照