(C++) const_cast
February 27, 2017 · View on GitHub
const_cast is a keyword to cast away constness.
Advice
- Avoid casting constness away [1, 3-4]
- const_cast may be used when using a non-const-correct API [1]
- const_cast may be used to avoid duplication in const and non-const member functions [1, 2].
References
- Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 94: 'Avoid casting away const'
- Scott Meyers. Effective C++ (3rd edition). ISBN:0-321-33487-6. Item 3, paragraph 'Avoid duplication in const and non-const member functions'
- John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 9.1.6: 'Think twice (at least) before casting away const'
- Gottschling, Peter. Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers. Addison-Wesley Professional, 2015. Chapter 5.2.3.2: 'Consider casting away const only as the very last resort'