skip to content

What does a C-style cast `(T)expr` actually do in C++, and why can it silently become a reinterpret_cast?

level: middleimportance: should knowfreq 55%

answer

  1. five interpretations, first one wins
  2. const, static, static+const, reinterpret
  3. no static_cast possible, fall through
  4. incomplete types: unspecified

basics

~10 s

A C-style cast tries const_cast, static_cast, static_cast plus const_cast, reinterpret_cast, then reinterpret_cast plus const_cast, and uses the first that applies. When no static_cast exists, it quietly reinterprets the address instead of failing.

solid answer

~40 s

In C++ the cast notation `(T)e`, and the functional form `T(e)`, is a search over five interpretations: `const_cast`, `static_cast`, `static_cast` then `const_cast`, `reinterpret_cast`, and `reinterpret_cast` then `const_cast`. The first one that applies is used, and nothing tells you which. So between unrelated class pointers, or from an integer to a pointer, where a named `static_cast` would fail to compile, the C-style cast becomes a `reinterpret_cast` and compiles. With forward-declared classes the standard leaves it unspecified whether the static or the reinterpret interpretation is used, so a base-class adjustment can be silently skipped. It can even reach an inaccessible base. Replace it with the named cast you meant, and use `-Wold-style-cast` to find them.

code

cpp · 18 lines
cpp
#include <cstdio>

struct Logger   { virtual ~Logger() = default;   int level = 1; };
struct Listener { virtual ~Listener() = default; int port = 7000; };
struct Plugin : Logger, Listener {};

int main() {
    Plugin plugin;
    Plugin* pp = &plugin;

    Listener* a = static_cast<Listener*>(pp);   // moves to the Listener subobject
    Listener* b = (Listener*)pp;                // C-style: static_cast interpretation
    std::printf("%d %d\n", a == b, b->port);    // 1 7000

    void* user = pp;                            // a C API stores the Plugin*
    Listener* c = (Listener*)user;              // static_cast from void*: no adjustment
    std::printf("%d\n", c == a);                // 0: c does not point at the Listener
}

go deeper

for a junior

Recall that a C-style cast tries several named casts in a fixed order, and that it can end up as a reinterpret_cast.

for a middle

Walk through the five interpretations and show a case where the named static_cast fails to compile but the C-style cast succeeds.

for a senior

When porting C plugin code, replace each C-style cast with the intended named cast and treat every resulting compile error as a found bug, especially with forward-declared types.

for a principal

Decide whether -Wold-style-cast becomes an error for all new code and how much legacy code is migrated, weighing churn against hidden reinterpretations.

## The cast notation is a search, not a conversion In C++, the C cast notation `(T)e`, and the functional notation `T(e)` with a single expression, which the draft defines as equivalent, do not name one conversion. The draft lists what the cast notation may perform, in order: 1. a `const_cast`; 2. a `static_cast`; 3. a `static_cast` followed by a `const_cast`; 4. a `reinterpret_cast`; 5. a `reinterpret_cast` followed by a `const_cast`. "If a conversion can be interpreted in more than one of the ways listed above, the interpretation that appears first in the list is used, even if a cast resulting from that interpretation is ill-formed." So the compiler takes the first interpretation that applies; it never tells you which one it chose. ## When the search lands on `reinterpret_cast` The danger is the fall-through to steps 4 and 5. Whenever no `static_cast` is possible, the C-style cast quietly becomes a `reinterpret_cast`, where a named `static_cast` would have been a compile error: - between pointers to **unrelated classes**, `(Listener*)some_logger` reinterprets the address instead of failing; - from an **integer to a pointer**, `(Session*)handle` compiles without comment; - with `const` in the way, step 5 also strips the qualifier, so `(char*)msg` removes `const` from a `const char*`. There is one more rule that only the cast notation has: it may perform the hierarchy conversions of `static_cast` **even to an inaccessible base class**, which a named `static_cast` refuses. ## Incomplete types: the unspecified case Plugin headers often forward-declare classes (`struct Plugin; struct Listener;`). For a cast between pointers to class types where one or both are **incomplete**, the draft says "it is unspecified whether the static_cast or the reinterpret_cast interpretation is used, even if there is an inheritance relationship between the two classes". ```cpp struct Plugin; // forward declarations only struct Listener; Listener* as_listener(Plugin* p) { return (Listener*)p; } // compiles silently ``` If `Plugin` actually derives from `Logger` and then `Listener`, the correct conversion adjusts the address; a reinterpretation does not. Apple clang 21 compiled the function above without a warning under `-Wall -Wextra`. The same line with `static_cast` is rejected ("static_cast from 'Plugin *' to 'Listener *', which are not related by inheritance, is not allowed") until both definitions are visible, which is exactly the error you want. ## `void*` round trips through the cast notation A C-style cast from `void*` finds `static_cast` at step 2, so `(Listener*)user` is `static_cast<Listener*>(user)`. That conversion only restores the type that was stored. If the plugin stored a `Plugin*`, the result has the `Plugin` object's address, while the `Listener` subobject of a class with two polymorphic bases sits elsewhere. The program in the example prints `0` for the address comparison: the C-style cast produced a pointer that does not point at the `Listener` subobject, and using it would be undefined behaviour. ## What to do instead | Intent | Write | |---|---| | value or hierarchy conversion | `static_cast` | | drop `const` for a read-only legacy API | `const_cast` | | address or integer reinterpretation | `reinterpret_cast`, with a comment | | run-time checked downcast | `dynamic_cast` | Two compiler switches help find the old notation: - GCC documents `-Wold-style-cast` as warning when "an old-style (C-style) cast to a non-void type is used within a C++ program", noting that the named casts are "much easier to search for"; - Clang accepts the same flag and reports "use of old-style cast". Casting to `void`, as in `(void)unused;`, is excluded by GCC's wording and remains the idiomatic way to discard a value. ## In an interview A strong answer also says why the rule exists: C++ inherited the notation from C, where a cast had only value and address meanings, and then added classes, inheritance, and `const` checking that a single notation cannot express safely. The named casts are the way to say which meaning you want. State the five-step order, then the consequence: the cast notation compiles in more situations than any single named cast, so it hides the one conversion you did not mean. A good answer ends with the repair, replacing each C-style cast in ported plugin code with the named cast that reproduces the intended step, and letting the compile errors show where the old code relied on a reinterpretation.

  • Is `int(x)` different from `(int)x` in C++?
    No. The draft defines a functional-notation conversion with a single parenthesized expression as equivalent to the corresponding cast expression, so `T(e)` performs the same five-step search. `T{e}` is different: it is list-initialization, which rejects narrowing conversions.
  • Why is `(void)result;` still acceptable in code that bans C-style casts?
    Converting to `void` only discards a value; there is no conversion to get wrong. GCC's `-Wold-style-cast` is documented for casts to a non-void type, so the idiom passes, and it is the usual way to silence an unused-value warning without `[[maybe_unused]]`.

  • A C-style cast in C++ is always a static_cast.
  • A C-style cast fails to compile when the types are unrelated.
  • (T)x and T(x) follow different conversion rules.
  • The compiler warns by default whenever a C-style cast picks reinterpret_cast.
  • Forward-declared classes are cast exactly like complete ones.