Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      C++ named requirements:NullablePointer(since C++11)

      From cppreference.com
      <cpp‎ |named req
       
       
      C++ named requirements
       

      Specifies that the type is a pointer-like object which can be compared tostd::nullptr_t objects.

      Contents

      [edit]Requirements

      The type must meet all of the following requirements:

      In addition, a value-initialized object of the type must produce a null value of that type. This null value shall only be equivalent to itself. Default initialization of the type may have anindeterminate or erroneous(since C++26) value.

      A value of the type must becontextually convertible tobool. The effect of this conversion returnsfalse if the value is equivalent to its null value andtrue otherwise.

      None of the operations that this type performs may throw exceptions.

      The type must satisfy the following additional expressions, given two valuesp andq that are of the type, and thatnp is a value ofstd::nullptr_t type (possibly const-qualified):

      Declaration Effects
      Type p(np);

      Type p= np;

      Afterwards,p is equivalent tonullptr
      Expression Effects
      Type(np) A temporary object that is equivalent tonullptr
      p= np Must return aType&, and afterwards,p is equivalent tonullptr
      p!= q

      Type and value meet theBooleanTestable requirements

      (until C++20)

      decltype(p!= q) modelsboolean-testable

      (since C++20)

      The effect is!(p== q)

      p== np

      np== p


      Type and value of both expressions theBooleanTestable requirements

      (until C++20)

      decltype(p== np) anddecltype(np== p) each modelboolean-testable

      (since C++20)

      The effect is(p== Type())

      p!= np

      np!= p


      Type and value of both expressions theBooleanTestable requirements

      (until C++20)

      decltype(p!= np) anddecltype(np!= p) each modelboolean-testable

      (since C++20)

      The effect is!(p== np)

      [edit]Notes

      Note that dereferencing (operator* oroperator->) is not required for aNullablePointer type. A minimalistic type that satisfies these requirements is

      class handle{int id=0;public:    handle()=default;    handle(std::nullptr_t){}explicit operatorbool()const{return id!=0;}friendbool operator==(handle l, handle r){return l.id== r.id;}friendbool operator!=(handle l, handle r){return!(l== r);}// or only a defaulted operator== (since C++20)};

      [edit]Standard library

      The following types satisfyNullablePointer:

      The following types must satisfyNullablePointer in order to communicate with standard library components:

      • The member typesX::pointer,X::const_pointer,X::void_pointer andX::const_void_pointer of everyAllocator typeX.
      • The member typepointer ofstd::unique_ptr.
      (since C++23)

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2114
      (P2167R3)
      C++11contextual convertibility tobool was too weak to reflect the expectation of implementationsrequirements strengthened
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/named_req/NullablePointer&oldid=176107"

      [8]ページ先頭

      ©2009-2025 Movatter.jp