r/cpp • • Jul 29 '26

const_cast: A Necessary Evil

https://www.elbeno.com/blog/?p=1858
70 Upvotes

106 comments sorted by

View all comments

Show parent comment

35

u/kisielk Jul 29 '26

unless you’re talking about bad C libraries, in which case why are you using them?

Sometimes you don't have a choice. Libraries are provided by vendors, customers, partners etc. You can try to get them to fix it but it's not always possible. I often have to use const_cast where a C library takes a raw non-const pointer to some array it doesn't actually mutate.

14

u/datnt84 Jul 29 '26

There is at least one Win32 function that is not const-correct where I needed a const-away cast.