I occasionally use 64 bit arithmetic in an open source C++ library of mine. I discovered that long long
serves my purpose quite nicely. Even some 10 year old solari
When your library is provided as source, one option is to provide a "porting" header, in which it is your users' responsibility to provide a 64 bit type (you'd specify the name). It's then also naturally their responsibility to deal with any compiler warnings that their choice of type provokes, either avoid them, suppress them, or ignore them.
I guess this is what you call "messing around with #defines", but I don't think there's too much wrong with it. You can provide a default version which just uses long long
directly and will work on your 10-year-old Solaris box and also on Windows, so most users would never need to go near the user-configurable part of your library.
Then for the pedantic users, you can provide a version for GCC which includes
and uses int64_t
instead of long long
. This doesn't provoke any warning for me with g++ -pedantic
. You could even do this in the default version by recognising GCC, which certainly is messing around with #defines, but again not in a way that's at all unusual for a multi-platform product.
If your library is also provided as binaries for certain platforms, then of course you have to decide what the 64 bit type is going to be. If it also appears in the library interface (and hence the header file), then you'll just have to choose one which will not provoke any warnings with reasonable compiler options. I think -pedantic
is a reasonable compiler option, and apparently so do your users, so again that's int64_t
on GCC.