Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is T(x) considered a cast?

Tags:

c++

casting

The following is a cast:

int foo = (int) somefloat;

However, is this considered a cast?

int foo = int( somefloat );

More importantly, if there is a difference between the two, is the resulting compiled code different?

like image 386
MarkP Avatar asked Oct 02 '12 14:10

MarkP


2 Answers

The second example is often called a function style cast and was added to C++ but there's no difference between the two in terms of semantics/object code.

Here's a good explanation of the reason that function style casts were added:

What exactly is or was the purpose of C++ function-style casts?

like image 98
Benj Avatar answered Nov 07 '22 09:11

Benj


There is no difference of result, however only first example can be used in C. In C++ you can use both.

like image 32
Zaffy Avatar answered Nov 07 '22 09:11

Zaffy