(C++) const
March 10, 2018 · View on GitHub
const (an abbreviation of 'constant') is a keyword to ensure read-only operations.
int main()
{
const int dozen = 12;
}
There are five types of const:
Using the const keyword, design flaws can be spotted at compile time.
Const is not deep as one can delete a pointer-to-const.
Active use of const is termed const correctness.
In function design consider using:
- const arguments, whenever these need to be read-only
- const variables, whenever these need not change after initialization
- In C++98 only: const return types
In class design consider using:
- const members: for member variables that must not change after construction
- const member functions: for member functions that must not change the class's state
Advice
- Prefer const data to mutable data [15]
- Use const whenever possible [1-6,14].
- Be const correct [7-10, 12]
- Prefer const over #defines [5]
- Avoid magic constants [13]
- Avoid constants at file scope in header files [11]
- Consider casting away const only as the very last resort [15]
External links
References
- [1] Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4 7.9.3: 'Use const extensively and consistently'.
- [2] Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 3: 'Use const whenever possible'.
- [3] Jarrod Hollingworth, Bob Swart, Mark Cashman, Paul Gustavson. Sams C++ Builder 6 Developer's Guide. ISBN: 0-672-32480-6. Chapter 3: 'Understand and use const in your code'.
- [4] Jesse Liberty. Sams teach yourself C++ in 24 hours. ISBN: 0-672-32224-2. Hour 8, chapter 'Const member functions': 'Use const whenever possible.'.
- [5] Scott Meyers. Effective C++ (3rd edition). ISBN: 0-321-33487-6. Item 2: 'Prefer consts, enums and inlines to #defines'.
- [6] Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 15: 'Use const proactively'.
- [7] Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 1 guideline: 'Be const correct'.
- [8] Marshall Cline, Greg Lomow and Mike Girou. C++ FAQs. ISBN: 0-201-3098301. FAQ 14.05: 'Is const correctness tedious?' (Answer: no).
- [9] The C++ FAQ Lite. Item 18.1: What is 'const correctness' (Answer: 'A good thing')?
- [10] Bruce Eckel. Thinking in C++, second edition, volume 1. 2000. ISBN: 0-13-979809-9. Item 8: 'Constants', paragraph 'Summary': 'const-correctness can be a lifesaver for projects'.
- [11] John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 2.3.3
- [12] John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 9.1.6: 'Every object in a system should be const-correct'
- [13] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 6.6. Advice. page 169: '[6] Avoid "magic constants"'
- [14] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 12.7. Advice. page 341: '[13] Use const extensively and consistently'
- [15] 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'
- [16] C++ Core Guidelines: P.10: Prefer immutable data to mutable data