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

后端 未结 14 2041
耶瑟儿~
耶瑟儿~ 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:25

    Do you necessarily want the smallest, as opposed to using int for types smaller than int?

    If not, and your compiler supports it, could you do:

    int main()
    {
        typeof('A') i_65 = 0; // declare variable 'i_65' of type 'char'
        typeof(10) i_10 = 0; // int
        typeof(10000) i_10000 = 0; // int
        typeof(1000000000000LL) i_1000000000000 = 0; // int 64
    }
    

提交回复
热议问题