Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::polymorphic

      From cppreference.com
      <cpp‎ |memory
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
       
       
      Defined in header<memory>
      template<class T,class Allocator=std::allocator<T>>
      class polymorphic;
      (1)(since C++26)
      namespace pmr{

         template<class T>
         using polymorphic=
              std::polymorphic<T,std::pmr::polymorphic_allocator<T>>;

      }
      (2)(since C++26)
      1)std::polymorphic is a wrapper containing dynamically-allocated object of possibly different types with value-like semantics.
      2)std::pmr::polymorphic is an alias template that uses apolymorphic allocator.

      Anstd::polymorphic object manages thelifetime of an owned object. The owned object is of typeT or any type (publicly and unambiguously) derived fromT. Anstd::polymorphic object can only have no owned object after it has been moved from, in this case it isvalueless .

      Every object of typestd::polymorphic<T, Allocator> uses an object of typeAllocator to allocate and free storage for the owned object as needed.

      If a program declares an explicit or partial specialization ofstd::polymorphic, the behavior is undefined.

      Contents

      [edit]Template parameters

      T - the type of the owned object
      Allocator - the type of the associated allocator
      Type requirements
      -
      T may be anincomplete type.
      -
      IfT is one of the following types, the program is ill-formed:
      -
      IfAllocator does not satisfy the requirements ofAllocator, the program is ill-formed.
      -
      Ifstd::allocator_traits<Allocator>::value_type is not the same type asT, the program is ill-formed.

      [edit]Nested types

      Type Definition
      value_typeT
      allocator_typeAllocator
      pointertypenamestd::allocator_traits<Allocator>::pointer
      const_pointertypenamestd::allocator_traits<Allocator>::const_pointer

      [edit]Data members

      Member Description
      Allocatoralloc the asociated allocator
      (exposition-only member object*)

      [edit]Member functions

      constructs thepolymorphic object
      (public member function)[edit]
      destroys the owned value, if there is one
      (public member function)[edit]
      assigns contents
      (public member function)[edit]
      Observers
      accesses the owned value
      (public member function)[edit]
      checks if thepolymorphic is valueless
      (public member function)[edit]
      returns the associated allocator
      (public member function)[edit]
      Modifiers
      exchanges the contents
      (public member function)[edit]

      [edit]Non-member functions

      specializes thestd::swap algorithm
      (function template)[edit]

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_polymorphic202502L(C++26)std::polymorphic

      [edit]Example

      This section is incomplete
      Reason: no example

      [edit]See also

      (C++26)
      a wrapper containing dynamically-allocated object with value-like semantics
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/polymorphic&oldid=182589"

      [8]ページ先頭

      ©2009-2025 Movatter.jp