Files
KDToolBox/cpp/propagate_const
Allen Winter 52611ec317 Standardize project to KDAB copyright policy
For individual files: use year of file creation
For entire project:
  remove year from the statement
  use the © where feasible

reference:
https://matija.suklje.name/how-and-why-to-properly-write-copyright-statements-in-your-code
2024-01-19 15:51:29 -05:00
..
2023-10-22 17:10:22 +02:00
2023-10-22 18:12:28 +02:00

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:

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.