How it works...

The explicit type conversion, sometimes referred to as C-style casting or static casting, is a legacy of compatibility of C++ with the C language and it enables you to perform various conversions:

  • Between arithmetical types
  • Between pointer types
  • Between integral and pointer types
  • Between const or volatile qualified and unqualified types
  • Any combination of the previous one and any of the preceding conversions

This type of casting does not work well with polymorphic types or in templates. Because of this, C++ provides the four casts we saw in the examples earlier. Using these casts leads to several important benefits:

  • They express user intent better, both to the compiler and others that read the code.
  • They enable safer conversion ...

Get Modern C++ Programming Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.