If I were to do:
int imin = std::numeric_limits<int>::min();
int imax = std::numeric_limits<int>::max();
will I get the same value regardless of which compiler/OS, or are these values implementation dependent?
If they are compiler/os dependent, would using my own constants:
static unsigned short MAX_UNSIGNED_SHORT = 65535;
static uint64_t MAX_UNSIGNED_64LONG = 18446744073709551615u;
cause issues; say on a 32 bit machine?
These values are implementation dependent (See the documentation for std::numeric_limits here.) However, they are guaranteed by the standard be able to represent values up to a certain value (see here for specific details.)
Using your own constants should never cause issues as long as the constants fall within the permitted representable values for that type. In your example the value for MAX_UNSIGNED_SHORT and MAX_UNSIGNED_64LONG both fall within the values for their respective types. They should be fine.
On another note, if you use fixed width integer types the result of std::numeric_limits::max should remain the same across implementations for the specfic type. (e.g std::numeric_limits<uint16_t>::max() will return the same value across platforms, and std::numeric_limits<uint64_t>::max() will return the same value across platforms, but std::numeric_limits<uint16_t>::max() != std::numeric_limits<uint64_t>::max().)
However, as @rci pointed out, fixed width types are optional according to the standard, therefore they aren't guaranteed to be present in all implementations.
They are implementation dependent, int is defined by the standard as being at least as long as short, as well as having at least sixteen bits. Everything else is at the discretion of the implementation. Nowadays, you'll generally see 32 bits for integer. http://www.cplusplus.com/doc/tutorial/variables/
It's hard to say whether it would cause issues, it depends on the details. You could safely use the values for 16 bits. It's generally not a good idea to hand define your own limits.
Honestly, the best thing to do is #include cstdint and simply use the fixed size types, e.g. int32_t, which have to be a specific size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With