What is the difference between C-like casting and functional casting? [duplicate]

Apparently I was wrong in my initial cut at an answer. They are roughly equivalent. And while compound type names like long long or void * can’t use functional syntax directly (i.e. long long(val) doesn’t work), using typedef can get around this issue.

Both cast notations are very bad and should be avoided. For example:

const char c="a";
void *fred = (void *)(&c);

works, and it shouldn’t.

Both the C-style cast notation will sometimes behave like static_cast, sometimes like const_cast, sometimes like reinterpret_cast, or even a combination of the two depending on the exact situation in which it’s used. These semantics are rather complex and it’s not always easy to tell exactly what’s happening in any given situation.

I have gone to using mostly C++ static_cast<type>(val) style casts, and never use C-style casts. Based on my research for this question I’m going to also stop using function-style casts for anything. The question “C++ cast syntax styles” has an excellent answer (the accepted one) that details why.

Leave a Comment