How does delete deal with pointer constness?

前端 未结 5 1909
不知归路
不知归路 2021-01-18 00:30

I was reading this question Deleting a const pointer and wanted to know more about delete behavior. Now, as per my understanding:

delete expressio

5条回答
  •  逝去的感伤
    2021-01-18 00:58

    operator delete accepts a void*. As part of a test program I overloaded operator delete and found that operator delete doesn't accept const pointer.

    How did you try this? It certainly does accept const pointers:

    #include 
    
    int main() {
        void* const px = 0;
        delete px;
        ::operator delete(px);
    }
    

    This code is correct, compiles (albeit with a justified warning) and executes.

    EDIT: Reading the original article – you aren't talking about a const pointer but a pointer to const, which is something else. The reason why this has to work is described there. As for why it's working: others have said this.

提交回复
热议问题