C++ Template Metaprogramming
Solutions to the exercises throughout the book
All Classes Namespaces Files Functions Variables Typedefs Macros Modules Pages
exercise_2_2::cpp20 Namespace Reference

Encapsulate solution for Exercise 2-2 (C++20). More...

Functions

template<typename Target , typename Source >
Target polymorphic_downcast (Source *const x)
 Check the validity of a downcast.
 
template<typename Target , typename Source >
Target polymorphic_downcast (Source &x)
 Check the validity of a downcast.
 

Detailed Description

Encapsulate solution for Exercise 2-2 (C++20).

Function Documentation

◆ polymorphic_downcast() [1/2]

template<typename Target , typename Source >
Target exercise_2_2::cpp20::polymorphic_downcast ( Source &  x)
inline

Check the validity of a downcast.

This method asserts (in debug builds only) that the downcast was legal. This overload accepts a reference type.

Template Parameters
TargetThe resulting type of the cast.
SourceThe input datatype (wihtout reference). Do not specify this explicitly: it should be deduced by the compiler.
Parameters
[in]xAn input reference type.
Returns
A dynamically casted instance from the source type.

Definition at line 57 of file c++20/exercise-2-2.hpp.

◆ polymorphic_downcast() [2/2]

template<typename Target , typename Source >
Target exercise_2_2::cpp20::polymorphic_downcast ( Source *const  x)
inline

Check the validity of a downcast.

This method asserts (in debug builds only) that the downcast was legal. This overload accepts a pointer type.

Template Parameters
TargetThe resulting type of the cast.
SourceThe input datatype (wihtout pointer). Do not specify this explicitly: it should be deduced by the compiler.
Parameters
[in]xAn input pointer type.
Returns
A dynamically casted instance from the source type.

Definition at line 37 of file c++20/exercise-2-2.hpp.