From Jason Turner
Watch on YouTube

Summary

This episode explains why using const_cast in C++ is dangerous and should be avoided. Jason demonstrates that modifying const objects leads to undefined behavior by showing how compilers can optimize away changes made through const_cast. He debunks common excuses for using const_cast, showing that standard APIs like POSIX and Win32 are already const-correct, and provides alternative approaches for the Meyer's implementation pattern using modern C++ techniques with templates and decltype(auto). Even in rare cases where const_cast might seem necessary, Jason recommends safer alternatives like making copies of const data before passing to untrusted functions.

Resources

Tags