mirror of
https://github.com/allankoechke/KDToolBox.git
synced 2026-12-01 01:04:19 +00:00
Change-Id: Id3fa86c7026c2aa852abcb2e36779328e875d588 Reviewed-on: https://codereview.kdab.com/c/kdab/KDToolBox/+/132635 Reviewed-by: Allen Winter <allen@kdab.com>
32 lines
932 B
Markdown
32 lines
932 B
Markdown
# propagate_const
|
|
|
|
**
|
|
A minimal reimplementation of `std::experimental_::propagate_const` from
|
|
the C++ Extensions for Library Fundamentals version 3.
|
|
**
|
|
|
|
The propagate_const class is a deep-const wrapper for pointers and
|
|
smart pointers. If you have a class and want to ensure const
|
|
correctness even for its data members that are held by (smart)
|
|
pointer, wrap them in a propagate_const:
|
|
|
|
```cpp
|
|
class MyClass {
|
|
KDToolBox::propagate_const<Data *> m_data;
|
|
|
|
public:
|
|
void observer() const {
|
|
m_data->mutate(); // ERROR
|
|
}
|
|
};
|
|
```
|
|
|
|
"Ordinary" pointers (raw and smart) do not deeply propagate const, and
|
|
would let the user accidentally (or deliberately) mutate m_data above.
|
|
If the state of m_data logically belongs to the observable state of
|
|
`*this`, then the ordinary (smart) pointer would violate const
|
|
correctness. The usage of `propagate_const` prevents that mistake from
|
|
happening.
|
|
|
|
Requires a C++17 capable compiler.
|