GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::polymorphic::swap (非メンバ関数) - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. memory
  3. polymorphic
  4. swap (非メンバ関数)

最終更新日時: 2026年07月23日 11時38分31秒 (JST)
Raclamusi が更新

履歴 編集

function
<memory>

std::polymorphic::swap (非メンバ関数)(C++26)

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);
}

出力

バージョン

言語

  • C++26

処理系

  • Clang: 22 ❌
  • GCC: 16.1 ✅
  • Visual C++: 2026 Update 2 ❌

関連項目

  • std::polymorphic

参照

  • P3019R14 indirect and polymorphic: Vocabulary Types for Composite Class Design
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.