There are many questions discussing the details of C and C++ dealing with pointer-to-const deletion, namely that free()
does not accept them and that delete
and delete[]
do and that constness doesn't prevent object destruction.
What I am interested on is whether you think it is a good practice to do so, not what the languages (C and C++) allow.
Arguments for pointer-to-const deletion include:
- Linus Torvalds'
kfree()
, unlike C'sfree()
, takes avoid const*
argument because he thinks that freeing the memory does not affect what is pointed to. free()
was designed before the introduction of the const keyword.- C++'s delete operators allow deletion of const data.
Arguments against it include:
- Programmers do not expect data to be modified (or deleted) when they pass a pointer-to-const to it.
- Many think that pointer-to-const implies not getting ownership of the data (but not that non-const would imply getting ownership).
- This is the common practice seen in most libraries and existing code.
Please argument well in your responses and possibly refer to authorities. My intention is not to start a poll here.