简体   繁体   中英

long long to int and int to long long conversion

I wonder if conversion like:

int to long

int to long long

long to int

long long to int

long long to long

long to long long

and

long double to double

double to long double

(and analogously for unsigned) are expensive operations. Should we worry about that conversion and define proper types for assignment? (Of course there is a risk of loss precision but I don't write about it. I'm asking about performance).

For example if I am sure that number can be store in int type. And I have to assign something in function like: void writeToX(long long& x) .

Is it important to pass to function long long x instead of int x ?

You should always strive to avoid type conversions, as they are normally indicative of poor programming style.

This is irrespective of the expense of a conversion operation, but do note that some of these conversions that you have listed - particularly to and from floating point to integral types - are not particularly cheap.

In C++, the behaviour on converting to a type that is not capable of holding the value is often undefined. So a safe conversion often needs to be implemented with size checking, which only adds to the expense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM