最終更新日時(UTC):
が更新

履歴 編集

function
<memory>

std::polymorphic::swap(C++26)

constexpr void swap(polymorphic& other) noexcept(see below);

概要

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

事前条件

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

効果

*thisotherの状態を交換する。propagate_on_container_swap::valuetrueの場合はアロケータも交換する。

例外

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

noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value ||
         allocator_traits<Allocator>::is_always_equal::value)

#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>};
  a.swap(b);
  assert(a->f() == 2 && b->f() == 1);
}

出力

バージョン

言語

  • C++26

処理系

関連項目

参照