> 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-04.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-04

Don't cast away const or volatile

* **Classification:** *Required*
* **Offers Fixes:** *No*

## What it does?

This check flags all uses of const\_cast or C-style casts that remove const or volatile qualification.

## Why is this bad?

Modifying a variable that was declared const is ***undefined behavior***, even with *const\_cast*.

## Example

```cpp
void fun_1() 
{ 
  const MyClass a1{};
  MyClass* a2 = const_cast<MyClass*>(&a1);  // Non-compliant
  MyClass* a3 = (MyClass*)&a1;  // Non-compliant
}
```

### See also

* Autosar March 2019: A5-2-3 A cast shall not remove any const or volatile qualification from the type of a pointer or reference.
* [C++ Core Guidelines: ES.50: Don’t cast away const.](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-casts-const)
