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 |
experimental::propagate_const | ||||
Member functions | ||||
Observers | ||||
Non-member functions | ||||
Helper classes | ||||
Defined in header <experimental/propagate_const> | ||
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 |
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:
Expression | Return type | Pre-conditions | Operational semantics |
---|---|---|---|
t.get() | element_type* | ||
ct.get() | element_type* orconst element_type* | t.get()== ct.get() | |
*t | element_type& | t.get()!= nullptr | *t refers to the same object as*(t.get()) |
*ct | element_type& orconst element_type& | ct.get()!= nullptr | *ct refers to the same object as*(ct.get()) |
t.operator->() | element_type* | t.get()!= nullptr | t.operator->()== t.get() |
ct.operator->() | element_type* orconst element_type* | ct.get()!= nullptr | ct.operator->()== ct.get() |
(bool)t | bool | (bool)t is equivalent tot.get()!= nullptr | |
(bool)ct | bool | (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().
Member type | Definition |
element_type | std::remove_reference_t<decltype(*std::declval<T&>())>, the type of the object pointed to byT |
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] |
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] |
hash support forpropagate_const (class template specialization)[edit] | |
specializations of the standard comparison function objects forpropagate_const (class template specialization)[edit] |
#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)
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3136 | LFTSv2 | meaninglessT likeint*const,void*, orconst PtrLike were allowed | disallowed |