MarkP
MarkP

Reputation: 4288

Is T(x) considered a cast?

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?

Upvotes: 7

Views: 180

Answers (3)

user529758
user529758

Reputation:

Yes, this is also a cast. C++ enables this style of casting, C only has (type)expression format casts. They're equivalent.

Upvotes: 1

user823738
user823738

Reputation: 17521

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

Upvotes: 2

Benj
Benj

Reputation: 32398

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?

Upvotes: 5

Related Questions