Simple question regarding type conversion.

This is quite basic regarding converting types. Can someone help me understand the difference between these two statements assuming x1 and x2 are of type DOUBLE..

1
2
int t = x1 + x2;
int t = int(x1 + x2);


Are they equivalent? Is the latter better practice for some reason? (I ask because way 2 is the example given in my book but I'm wondering why you wouldn't do it the first way to save typing as much).
I think they are equivalent. Anyone else?
> Are they equivalent?

Yes, in effect. Both require a narrowing conversion from double to int.
int t = x1 + x2; results in an implicit conversion from double to int.
int t = int(x1 + x2); the conversion is explicit (functional cast notation).

There would be a difference if the expression x1 + x2 appeared in a braced-init-list - implicit narrowing conversions are not allowed in {} initialization
http://www.stroustrup.com/C++11FAQ.html#narrowing
http://coliru.stacked-crooked.com/a/3c566e5ca3e334bc
Thanks for the help
Topic archived. No new replies allowed.