Automatically pick a variable type big enough to hold a specified number

后端 未结 14 2018
耶瑟儿~
耶瑟儿~ 2020-12-07 14:01

Is there any way in C++ define a type that is big enough to hold at most a specific number, presumably using some clever template code. For example I want to be able to writ

14条回答
  •  不思量自难忘°
    2020-12-07 14:40

    I'm a bit late but...

    #include 
    #include 
    #include 
    
    template
    struct getMinimalByteSize{
        using type= typename std::conditional< (bool)(data & (uint64_t)0xFFL << (test_bit*8)),
            typename std::tuple_element_t>,
            typename getMinimalByteSize::type>::type;};
    
    template
    struct getMinimalByteSize{using type = uint64_t;};
    
    int main()
    {
      static_assert(sizeof(getMinimalByteSize<0x0>::type)==8);
      static_assert(sizeof(getMinimalByteSize<0xFF>::type)==1);
      static_assert(sizeof(getMinimalByteSize<0xFFF>::type)==2);
      static_assert(sizeof(getMinimalByteSize<0xFFFFF>::type)==4);
      static_assert(sizeof(getMinimalByteSize<0xFFFFFFFFF>::type)==8);
    }
    

    The difference with all the other methods is on the testing. Instead of testing if the value is bigger than the biggest number possible given N bits, it goes byte for byte, testing if it is the last (most significant) non zero byte. If it is, then this is the minimal number of bits needed. Lastly we use a hand made list to fix the fact that there are not 24, 48, 56 bit integers defined in C++.

    This is how this template metaprogram would look as a simple C function:

    #include 
    
    int tuple_element_t[]={8,16,32,32,64,64,64,64,64};
    
    int getMinimalByteSize(uint64_t data, int8_t first_hi_byte = sizeof(data)-1){
        if (!data) return 0;
        /* Does the N bit of test is set? If so, we are done*/
        if (data &  (uint64_t)0xFFL << (first_hi_byte*8))
            return tuple_element_t[first_hi_byte];
        else/*Else, we tray with the next bit*/
            return getMinimalByteSize(data, first_hi_byte-1);}
    

    Don't worry if you don't see it the first time, give yourself time . I've being working on AVRs for more than 10 years, in a platform where every byte counts. If you understand it in less than those 10 years, you already beat my.

提交回复
热议问题