Skip to content

Latest commit

 

History

History
69 lines (50 loc) · 1.54 KB

File metadata and controls

69 lines (50 loc) · 1.54 KB

swap (非メンバ関数)

  • memory[meta header]
  • std[meta namespace]
  • polymorphic[meta class]
  • function[meta id-type]
  • cpp26[meta cpp]
friend constexpr void swap(polymorphic& lhs, polymorphic& rhs) noexcept(see below);

概要

2つのpolymorphicオブジェクトを交換する。Hidden friendsとして定義される。

効果

lhs.swap(rhs)と等価。

例外

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

noexcept(noexcept(lhs.swap(rhs)))

#include <cassert>
#include <memory>

struct Base { virtual ~Base() = default; virtual int f() const = 0; };
struct D1 : Base { int f() const override { return 1; } };
struct D2 : Base { int f() const override { return 2; } };

int main()
{
  std::polymorphic<Base> a{std::in_place_type<D1>};
  std::polymorphic<Base> b{std::in_place_type<D2>};
  swap(a, b);   // ADLにより非メンバswapが呼ばれる
  assert(a->f() == 2 && b->f() == 1);
}
  • std::polymorphic[link ../polymorphic.md]
  • std::in_place_type[link /reference/utility/in_place_type_t.md]
  • swap[color ff0000]

出力

バージョン

言語

  • C++26

処理系

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

関連項目

参照