> For the complete documentation index, see [llms.txt](https://cpp-clippy.gitbook.io/cpp-clippy/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cpp-clippy.gitbook.io/cpp-clippy/rules/critical/crt-exp-02.md). Agents can also search and read this documentation through its [MCP server](https://cpp-clippy.gitbook.io/cpp-clippy/~gitbook/mcp).

# Rule CRT-EXP-02

Do not use dynamic\_cast

* **Classification:** *Advisory*
* **Offers Fixes:** *No*

## What it does?

This check flags all use of *dynamic\_cast*.

## Why is this bad?

Implementations of dynamic\_cast mechanism are unsuitable for use with real-time systems where low memory usage and determined performance are essential.

If dynamic casting is essential for your program, usage of its custom implementation should be considered. Also, usage of the dynamic\_cast can be replaced with polymorphism, i.e. virtual functions.

## Example

```cpp
struct Base
{
    virtual ~Base() {}
};
 
struct Derived: Base
{
    virtual void name() {}
};
 
int main()
{
    Base* b = new Base;
    Derived* d = dynamic_cast<Derived*>(b);  // Non-Compliant
}
```

### See also

* JSF December 2005: AV Rule 178 Down casting (casting from base to derived class) shall only be allowed through one of the following mechanism: Virtual functions that act like dynamic casts (most likely useful in relatively simple cases); Use of the visitor (or similar) pattern (most likely useful in complicated cases).
* Autosar March 2019: A5-2-1 dynamic\_cast should not be used.
