Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/experimental/observer ptr"

From cppreference.com
(fixed typo)
m (fmt)
 
(5 intermediate revisions by 4 users not shown)
Line 3: Line 3:
 
{{dcl begin}}
 
{{dcl begin}}
 
{{dcl header|experimental/memory}}
 
{{dcl header|experimental/memory}}
{{dcl | since=libfund_ts_2 || 1=
+
{{dcl|since=libfund_ts_2||1=
 
template< class W >
 
template< class W >
 
class observer_ptr;
 
class observer_ptr;
Line 9: Line 9:
 
{{dcl end}}
 
{{dcl end}}
  
{{tt|std::experimental::observer_ptr}} is a non-owning pointer, or ''observer''. The observer stores a pointer to a second object, known as the ''watched object''. An {{tt|observer_ptr}} may also have no watched object.  
+
{{tt|std::experimental::observer_ptr}} is a non-owning pointer, or ''observer''. The observer stores a pointer to a second object, known as the ''watched object''. An {{tt|observer_ptr}} may also have no watched object.  
  
 
An observer is not responsible in any way for the watched object; there is no inherent relationship between an observer and the object it watches.
 
An observer is not responsible in any way for the watched object; there is no inherent relationship between an observer and the object it watches.
  
Specializations of {{tt|observer_ptr}} satisfy the requirements of {{concept|CopyConstructible}} and {{concept|CopyAssignable}}.
+
It is intended as a near drop-in replacement for raw pointer types, with the advantage that, as a vocabulary type, it indicates its intended use without need for detailed analysis by code readers.
 +
 
 +
Specializations of {{tt|observer_ptr}} satisfy the requirements of {{named req|CopyConstructible}} and {{named req|CopyAssignable}}.
  
 
{{par begin}}
 
{{par begin}}
 
{{par hreq}}
 
{{par hreq}}
{{par req | {{tt|W}} shall not be a reference type, but may be an incomplete type. }}
+
{{par req|{{tt|W}} shall not be a reference type, but may be an incomplete type.}}
 
{{par end}}
 
{{par end}}
  
 
===Member types===
 
===Member types===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc hitem | Member type | Definition}}
+
{{dsc hitem|Member type|Definition}}
{{dsc | {{c|element_type}} | {{tt|W}}, the type of the object watched by this {{tt|observer_ptr}}}}
+
{{dsc|{{c|element_type}}|{{tt|W}}, the type of the object watched by this {{tt|observer_ptr}}}}
{{dsc | {{c|pointer}} | {{c|std::add_pointer_t<W>}}. For exposition only.}}
+
 
{{dsc end}}
 
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/experimental/observer_ptr/dsc constructor}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc constructor}}
{{dsc inc | cpp/experimental/observer_ptr/dsc destructor}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc destructor}}
{{dsc inc | cpp/experimental/observer_ptr/dsc operator{{=}}}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc operator{{=}}}}
  
{{dsc h2 | Modifiers}}
+
{{dsc h2|Modifiers}}
{{dsc inc | cpp/experimental/observer_ptr/dsc release}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc release}}
{{dsc inc | cpp/experimental/observer_ptr/dsc reset}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc reset}}
{{dsc inc | cpp/experimental/observer_ptr/dsc swap}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc swap}}
  
{{dsc h2 | Observers}}
+
{{dsc h2|Observers}}
{{dsc inc | cpp/experimental/observer_ptr/dsc get}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc get}}
{{dsc inc | cpp/experimental/observer_ptr/dsc operator bool}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc operator bool}}
{{dsc inc | cpp/experimental/observer_ptr/dsc operator*}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc operator*}}
 
+
{{dsc h2 | Conversions}}
+
{{dsc inc | cpp/experimental/observer_ptr/dsc operator pointer}}
+
  
 +
{{dsc h2|Conversions}}
 +
{{dsc inc|cpp/experimental/observer_ptr/dsc operator pointer}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Non-member functions===
 
===Non-member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/experimental/observer_ptr/dsc make_observer}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc make_observer}}
{{dsc inc | cpp/experimental/observer_ptr/dsc operator_cmp}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc operator_cmp}}
{{dsc inc | cpp/experimental/observer_ptr/dsc swap2}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc swap2}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Helper classes===
 
===Helper classes===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/experimental/observer_ptr/dsc hash}}
+
{{dsc inc|cpp/experimental/observer_ptr/dsc hash}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Example===
 
===Example===
{{example |
+
{{example}}
| code=
+
 
| output=
+
{{langlinks|ru|zh}}
}}
+

Latest revision as of 01:11, 28 November 2023

 
 
Experimental
Technical Specification
Filesystem library (filesystem TS)
Library fundamentals (library fundamentals TS)
Library fundamentals 2 (library fundamentals TS v2)
Library fundamentals 3 (library fundamentals TS v3)
Extensions for parallelism (parallelism TS)
Extensions for parallelism 2 (parallelism TS v2)
Extensions for concurrency (concurrency TS)
Extensions for concurrency 2 (concurrency TS v2)
Concepts (concepts TS)
Ranges (ranges TS)
Reflection (reflection TS)
Mathematical special functions (special functions TR)
Experimental Non-TS
Pattern Matching
Linear Algebra
std::execution
Contracts
2D Graphics
 
 
 
Defined in header <experimental/memory>
template< class W >
class observer_ptr;
(library fundamentals TS v2)

std::experimental::observer_ptr is a non-owning pointer, or observer. The observer stores a pointer to a second object, known as the watched object. An observer_ptr may also have no watched object.

An observer is not responsible in any way for the watched object; there is no inherent relationship between an observer and the object it watches.

It is intended as a near drop-in replacement for raw pointer types, with the advantage that, as a vocabulary type, it indicates its intended use without need for detailed analysis by code readers.

Specializations of observer_ptr satisfy the requirements of CopyConstructible and CopyAssignable.

Type requirements
-
W shall not be a reference type, but may be an incomplete type.

Contents

[edit] Member types

Member type Definition
element_type W, the type of the object watched by this observer_ptr

[edit] Member functions

constructs a new observer_ptr
(public member function) [edit]
(destructor)
(implicitly declared)
destructs an observer_ptr
(public member function) [edit]
operator=
(implicitly declared)
implicitly declared copy and move assignment operators that assign the stored pointer
(public member function) [edit]
Modifiers
returns a pointer to the watched object and stops watching the object
(public member function) [edit]
replaces the watched object
(public member function) [edit]
swaps the watched objects
(public member function) [edit]
Observers
returns a pointer to the watched object
(public member function) [edit]
checks if there is an associated watched object
(public member function) [edit]
dereferences pointer to the watched object
(public member function) [edit]
Conversions
explicit conversion function to the stored pointer
(public member function) [edit]

[edit] Non-member functions

creates an observer_ptr that watches an object
(function template) [edit]
compares to another observer_ptr or with nullptr
(function template) [edit]
specializes the swap algorithm
(function template) [edit]

[edit] Helper classes

hash support for observer_ptr
(class template specialization) [edit]

[edit] Example