tags:

views:

122

answers:

2

Where to check if type long long is defined? I wanna do something like this:

#ifdef LONGLONG
#define long_long long long
#else 
#define long_long long
#endif
+6  A: 

LLONG_MAX gives the maximum value representable by a long long; if your implementation doesn't support long long, it shouldn't define LLONG_MAX.

#include <limits.h>

#ifdef LLONG_MAX
#define long_long long long
#elif 
#define long_long long
#endif

This isn't a perfect solution. long long isn't standard in C++03, and long long has been around longer than C99, so it's possible (and likely) that a compiler could support long long but not define LLONG_MAX.

If you want an integer type with a specific size, you should use <stdint.h> if your implementation supports it. If your implementation doesn't support it, Boost has an implementation of it.

James McNellis
@James my implementation supports (provides) stdint.h, I've found there something like this: #define _LONGLONG __int64, so I understand that I shall type _LONGLONG instead of LLONG_MAX and it should be ok? Really appreciate if you could answer that. Thank you.
There is nothing we can do
@Knowing: Why not just use the types defined in stdint.h? `_LONGLONG` is defined by your implementation and is not standard, so if you want to write portable code you should not use it.
James McNellis
@James McNellis I found there (stdint.h) something like this: UINT64_MAX, if I use this I think it will be ok?
There is nothing we can do
@Knowing: No... `UINT64_MAX` gives you the maximum value representable by a `uint64_t`, not a `long long`. What are you trying to do?
James McNellis
@James I'm trying to get as a result from my fnc largest possible type available.
There is nothing we can do
@Knowing: Then use `std::intmax_t` declared in `<cstdint>`. That will be the largest signed integral type available.
James McNellis
+1  A: 

Rather than worry about whether or not a type with that name is defined, I'd #include <climits> and check whether or not you can find an integer type large enough for your intended use. (Although you could probably just check if LLONG_MAX is defined to find out if long long exists.)

Edit: Or, if you can assume C99 headers and types to be available, #include <cstdint.h> and use e.g. int64_t to get a 64-bit type or int_fast64_t to get a “fast” 64-bit type (by some compiler-specific definition of fast). Or intmax_t if you want the largest available type.

Arkku