What has narrowing conversion and casts in common? They are often the source of errors; therefore, I will write about them today.
Here are the rules from the guidelines.
- ES.46: Avoid narrowing conversions
- ES.48: Avoid casts
- ES.49: If you must use a cast, use a named cast
- ES.50: Don’t cast away
- ES.55: Avoid the need for range checking
Narrowing conversion is a conversion of a value, including the loss of its precision. Most of the time that is not what you want.
Here are a few examples from the guidelines.
If you want a narrowing conversion, you should do it explicitly, not implicitly, according to the Python rule from The Zen of Python: Explicit is better than implicit. The guideline support library (GSL) has two casts to express your intent: gsl::narrow_cast and gsl::narrow.
The gsl::narrow_cast performs the cast, and the gsl::narrow cast throws an exception if a narrowing conversion happens.
Most of the time, a narrowing conversion happened secretly. How can you protect yourself from this? Use the power of the curly braces:
All initializations are put into curly braces. According to the C++11 standard, the compiler has to warn you if a narrowing conversion happens.
Explicit is better than implicit. This will not hold a C-cast.
Let’s see what will happen if we screw up the type of system.
Neither the result with the Visual Studio compiler
nor the result with the GCC or the clang compiler is promising.
What is bad about the C-cast? You don’t see which cast is performed. If you perform a C-cast, a combination of casts will be applied if necessary. Roughly speaking, a C-cast starts with a static_cast, continues with a const_cast, and finally performs a reinterpret_cast.
Of course, you know how I will continue: explicit is better than implicit.
Including the GSL, C++ offers eight different named casts. Here are including a short description:
- static_cast: conversion between similar types such as pointer types or numeric types
- const_cast: adds or removes const or volatile
- reinterpret_cast: converts between pointers or between integral types and pointers
- dynamic_ cast: converts between polymorph pointers or references in the same class hierarchy
- std::move: converts to an rvalue reference
- std::forward: converts to an rvalue reference
- gsl::narrow_cast: applies a static_cast
- gsl::narrow: applies a static_cast
What? std::move and std::forward are casts? Le’s have a closer look at the internals of std::move:
First, the type of argument arg is determined by decltype(arg). Then all references are removed, and two new references are added. The function std::remove_reference is from the type-traits library. I have already written a few posts to the type-traits library. In the end, we will always get an rvalue reference.
Casting away const is undefined behavior.
Let me be more specific. Casting away const is undefined behavior if the underlying object, such as constInt is not mutable.
If you don’t believe me, there is a footnote in the C standard [ISO/IEC 9899:2011] (subclause 6.7.3, paragraph 4) relevant to the C++ standard: The implementation may place a const object that is not volatile in a read-only region of storage. Moreover, the implementation need not allocate storage for such an object if its address is never used.
Did I mention mutable? mutable is one of the unknown features in C++. mutable allows you to differentiate between bitwise and logical constness. What?
Imagine you want to implement the interface to a telephone book. For simplicity reasons, the entries should be in a std::unordered_map.
My telephone book (1) is tiny. Usually, a telephone book is quite big, and updating it is quite expensive (2). This means updating a printed telephone book will happen only once a year in Germany. From the conceptional view, the inquiries to the teleBook (3) should be const. This is not possible because the unordered_map is modified in the method getNumber. Here is the proof in red ellipses.
The qualifier mutable allows you to differentiate between bitwise and logical constness. The telBook is logical but not bitwise const.
I just added const (3) to the telBook and mutable to the cache (1), and the program behaves as expected.
I can make it short. By using the range-based for-loop or algorithms of the STL, there is no need to check the range.
In the next post on expressions, I will write about std::move, new and delete, and slicing. Slicing is probably one of the darkest corners of C++. So, stay tuned.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, and Marco Parri Empoli.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,