There is a standard, patterned way to get the size of the types:

template <typename Type> size_t GetSize() { static_assert( false, "Unknown"); return 0; } template <> size_t GetSize< int32_t >() { return 32; } template <> size_t GetSize< char >() { return CHAR_BIT; } 

Or do you need to manually write for all types?

    1 answer 1

    sizeof(x) * CHAR_BIT enough, because sizeof(char) always 1.

    • If it is not right, correct: it is not always CHAR_BIT is 8 (maybe more), on some architectures the size of char may be equal to 2 and 4. - isnullxbh
    • @isnullxbh is not, although CHAR_BIT can be any (not less than 7), but the size of the char always 1, according to the standard. - Abyx
    • Which standard exactly? - isnullxbh
    • Actually, here is isnullxbh
    • one
      @isnullxbh we have ours - ru.stackoverflow.com/questions/118221/… - Abyx