Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::experimental::propagate_const

      From cppreference.com
      <cpp‎ |experimental
       
       
       
       
       
      template<class T>
      class propagate_const;
      (library fundamentals TS v2)

      std::experimental::propagate_const is a const-propagating wrapper for pointers and pointer-like objects. It treats the wrapped pointer as a pointer toconst when accessed through aconst access path, hence the name.

      The class satisfies the requirements ofMoveConstructible andMoveAssignable if the underlying pointer-like type satisfies the corresponding requirement, butpropagate_const is neitherCopyConstructible norCopyAssignable.

      Type requirements
      -
      T must be cv-unqualified pointer-to-object type or a cv-unqualified pointer-like class type, as specified below.

      Contents

      [edit]Requirements on pointer-like class types

      IfT is a class type, it must satisfy the requirements in this subsection.

      Given

      • t, a modifiablelvalue expression of typeT,
      • ct, an lvalue of typeconst T that denotes the same object ast (equivalent tostd::as_const(t) since C++17),
      • element_type, an object type.

      The following expressions must be valid and have their specified effects:

      ExpressionReturn typePre-conditionsOperational semantics
      t.get()element_type*
      ct.get()element_type* orconst element_type*t.get()== ct.get()
      *telement_type&t.get()!= nullptr*t refers to the same object as*(t.get())
      *ctelement_type& orconst element_type&ct.get()!= nullptr*ct refers to the same object as*(ct.get())
      t.operator->()element_type*t.get()!= nullptrt.operator->()== t.get()
      ct.operator->()element_type* orconst element_type*ct.get()!= nullptrct.operator->()== ct.get()
      (bool)tbool(bool)t is equivalent tot.get()!= nullptr
      (bool)ctbool(bool)ct is equivalent toct.get()!= nullptr

      Further,T andconst T shall be contextually convertible tobool.

      In addition, ifT is implicitly convertible toelement_type*, then(element_type*)t shall be equal tot.get(). Similarly, ifconst T is implicitly convertible toconst element_type*, then(const element_type*)ct shall be equal toct.get().

      [edit]Member types

      Member type Definition
      element_typestd::remove_reference_t<decltype(*std::declval<T&>())>, the type of the object pointed to byT

      [edit]Member functions

      constructs a newpropagate_const
      (public member function)[edit]
      (destructor)
      (implicitly declared)
      destructs apropagate_const, destroying the contained pointer
      (public member function)[edit]
      assigns thepropagate_const object
      (public member function)[edit]
      swaps the wrapped pointer
      (public member function)[edit]
      Observers
      returns a pointer to the object pointed to by the wrapped pointer
      (public member function)[edit]
      checks if the wrapped pointer is null
      (public member function)[edit]
      dereferences the wrapped pointer
      (public member function)[edit]
      implicit conversion function to pointer
      (public member function)[edit]

      [edit]Non-member functions

      compares to anotherpropagate_const, another pointer, or withnullptr
      (function template)[edit]
      specializes theswap algorithm
      (function template)[edit]
      retrieves a reference to the wrapped pointer-like object
      (function template)[edit]

      [edit]Helper classes

      hash support forpropagate_const
      (class template specialization)[edit]
      specializations of the standard comparison function objects forpropagate_const
      (class template specialization)[edit]

      [edit]Example

      Run this code
      #include <experimental/propagate_const>#include <iostream>#include <memory> struct X{void g()const{std::cout<<"X::g (const)\n";}void g(){std::cout<<"X::g (non-const)\n";}}; struct Y{    Y(): m_propConstX(std::make_unique<X>()), m_autoPtrX(std::make_unique<X>()){} void f()const{std::cout<<"Y::f (const)\n";        m_propConstX->g();        m_autoPtrX->g();} void f(){std::cout<<"Y::f (non-const)\n";        m_propConstX->g();        m_autoPtrX->g();}     std::experimental::propagate_const<std::unique_ptr<X>> m_propConstX;std::unique_ptr<X> m_autoPtrX;}; int main(){    Y y;    y.f(); const Y cy;    cy.f();}

      Output:

      Y::f (non-const)X::g (non-const)X::g (non-const)Y::f (const)X::g (const)X::g (non-const)

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3136LFTSv2meaninglessT likeint*const,void*, orconst PtrLike were alloweddisallowed
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/experimental/propagate_const&oldid=168558"

      [8]ページ先頭

      ©2009-2025 Movatter.jp