Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

casting

Consider:

b = (int) a;    // C-like cast notation
b = int (a);    // Functional notation
like image 635
nnkk Avatar asked Jan 23 '11 19:01

nnkk


People also ask

What is a C-style cast?

C-style casts can be used to convert any type into any other type, potentially with unsafe results (such as casting an integer into a pointer type). (<type>)<value> This example casts an int to a double for the purpose of avoiding truncation due to integer division: double result = (double)4/5; Popular pages.

Why is static_cast better than C-style cast?

In short: static_cast<>() gives you a compile time checking ability, C-Style cast doesn't. static_cast<>() is more readable and can be spotted easily anywhere inside a C++ source code, C_Style cast is'nt. Intentions are conveyed much better using C++ casts.

What is function style cast?

2) The functional cast consists of a simple type specifier or a typedef specifier (in other words, a single-word type name: unsigned int(expression) or int*(expression) are not valid), followed by a single expression in parentheses. This cast is exactly equivalent to the corresponding C-style cast expression.

What is a function style cast in C++?

The C++ cast operators are keywords defined in the language. While they look like template functions, they are part of the language itself, i.e.the behavior is implemented in the compiler, not in the standard library.


3 Answers

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.

like image 56
Omnifarious Avatar answered Oct 11 '22 20:10

Omnifarious


There's hardly any difference. Officially, the first tells the compiler that the value is an integer. This probably doesn't generate any extra code at all. The function call is an actual call that internally performs the other typecast. A smart compiler will optimize this, so they are actually the same.

like image 23
GolezTrol Avatar answered Oct 11 '22 18:10

GolezTrol


There isn't any difference. It is a matter of preference. These are old-style casts.

like image 24
Cratylus Avatar answered Oct 11 '22 19:10

Cratylus