Skip to content

Latest commit

 

History

History
75 lines (55 loc) · 1.66 KB

File metadata and controls

75 lines (55 loc) · 1.66 KB

operator*

  • memory[meta header]
  • std[meta namespace]
  • polymorphic[meta class]
  • function[meta id-type]
  • cpp26[meta cpp]
constexpr const T& operator*() const noexcept; // (1)
constexpr T& operator*() noexcept;             // (2)

概要

所有するオブジェクトへの、基底型Tとしての参照を取得する。constなアクセス経路ではconstが所有オブジェクトに伝播する。

事前条件

*thisが無効値状態でないこと。

戻り値

所有するオブジェクトへのT型としての参照。

例外

投げない。

#include <cassert>
#include <memory>

struct Base {
  virtual ~Base() = default;
  virtual int f() const { return 0; }
};
struct Derived : Base {
  int x = 42;
  Derived() = default;
  Derived(int x) : x(x) {}
  int f() const override { return x; }
};

int main()
{
  std::polymorphic<Base> a{std::in_place_type<Derived>};
  Base& r = *a;          // 基底型Baseとしての参照を取得
  assert(r.f() == 42);   // 仮想関数で派生型Derivedの実装が呼ばれる
}
  • std::polymorphic[link ../polymorphic.md]
  • std::in_place_type[link /reference/utility/in_place_type_t.md]

出力

バージョン

言語

  • C++26

処理系

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

関連項目

参照