Search code examples
c++vectorindexingbit-fields

uintmax_t for vector indexes


I am dealing with basically a bit-flag search mask and I'm using vectors. These indexes need to go up to the max integer on the machine (defined in stdint.h)

Basically the problem is

searchMask[ UINTMAX_MAX] = false; // or any value > unsigned int

results in the following warning

warning: C4244: 'argument' : conversion from 'uintmax_t' to 'unsigned int', 
possible loss of data

I have considered just using something like an unsigned char* = "1110010..." and just flipping the bits that way, but dealing with C-strings is always a pain and I suspect accessing the char array index will have this same size problem?

Can I make the indexes of the vector go off the uintmax_t, or should I go the C string route, or what?


Solution

  • Practically all the STL containers will use size_t as their size types. So, depending on your system, size_t might be defined as an unsigned int, which will probably be a 32-bit integer in your case. That would explain why the compiler is complaining.

    UINTMAX_MAX is defined as UINT64_MAX, so it won't fit in a 32-bit integer. Try using the UINT32_MAX macro, or be platform-independant and use std::numeric_limits<size_t>::max().

    Also, try using std::bitset<N>.