Is using const_cast for read-only access to a const object allowed?

前端 未结 5 1674
南方客
南方客 2020-12-11 18:41

In C++ I have a function that only requires read-only access to an array but is mistakenly declared as receiving a non-const pointer:

size_t countZeroes( int         


        
5条回答
  •  旧巷少年郎
    2020-12-11 19:10

    Since your code does not modify the array, and you told the compiler you know what you are doing by using the const_cast, you will actually be OK. However, I believe you are technically invoking undefined behaviour. Best to get the function declaration fixed, or write, declare and use the const-safe version of it.

提交回复
热议问题