jbcoe / propagate_const

A const-propagating member-pointer-wrapper for the C++ standard library
MIT License
20 stars 8 forks source link

Construct from a pointer-to-const #29

Closed Krantz-XRF closed 3 years ago

Krantz-XRF commented 4 years ago

I asked about a deep-const pointer implementation on Stack Overflow, and was guided to this proposal and implementation. What I needed is a "deep-const" pointer type P:

  1. Constant P is pointer-to-const;
  2. Mutable P is pointer-to-mutable;
  3. A const reference to a non-const P is treated as if it were a const P;
  4. Since pointer-to-const has value semantics, a const P should be trivially copyable to another const P.

Current propagate_const implementation fails to match the requirement because:

  1. It never accepts a pointer-to-const;
  2. It is not copyable (copy constructors explicitly deleted);

I wonder whether the limitations above of current propagate_const was by design, or due to limitations of the C++ language?

jbcoe commented 3 years ago

propagate_const is non-copyable by design and wont accept a pointer to const by design.

If propagate_const were copyable then 'const' could be dropped by copying the propagate const instance. See https://github.com/jbcoe/propagate_const/blob/master/notes_on_design/paper.md

propagate_const won't accept a pointer to const as its purpose is to propagate const-ness to pointees accessed through a const access path. If the pointee is unconditionally const then there is nothing for the class to do. Argaubly this could be permitted but it's probably not useful.

If you are looking for deep pointers, depending what you mean, indirect_value and polymorphic_value may be what you need:

https://github.com/jbcoe/polymorphic_value https://github.com/jbcoe/indirect_value

Both are being considered for addition to a future C++ standard.