开发者

Why might using a "long long" in C or C++ be a bad thing?

Why might using a "long long" in C or C++ be a bad thing?

I was compiling a runtime library the other day and in the code it checks to see if longs are 64bits and if not it uses a long long. But along with that, it sends out a #warning "using long long". I can't 开发者_如何学运维think of any reason for the "long long" being a warning unless it was leftover debug cruft from the developer.

Thanks Chenz


As far as I know, long long is currently standard only in C99. It will also be a type in C++0x, but most modern compilers should support it already.

However, for fixed-sized integers one might use the C99 header <stdint.h>, or in C++ <boost/cstdint.hpp>


Two reasons:

You don't know how long (haha!) a long long is, so if the code assumes that it is exactly 64 bits, there could be a problem.

If you use an old C compiler, it might not support long long.


I agree with Thomas Padron-McCarth. It is much better to check for the existence of int64_t (signed) or uint64_t (unsigned) respectively, and use them if they exist. It's the same sort of sanity that led to size_t.


It isn't portable. Specifically, the windows compiler didn't use to support long long, and you had to use __int64 and unsigned __int64 instead (but couldn't use necessarily use on non-windows platforms).

This was a while ago, so perhaps now there is a better chance of always having uint64_t and int64_t available.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜