Files
KDToolBox/cpp/propagate_const/README.md
Allen Winter 0d9f524da7 markdown clean all the README.md's
Change-Id: Id3fa86c7026c2aa852abcb2e36779328e875d588
Reviewed-on: https://codereview.kdab.com/c/kdab/KDToolBox/+/132635
Reviewed-by: Allen Winter <allen@kdab.com>
2023-10-22 18:12:28 +02:00

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.