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

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

    Boost.Integer already has facilities for Integer Type Selection:

    boost::int_max_value_t::least
    

    The smallest, built-in, signed integral type that can hold all the values in the inclusive range 0 - V. The parameter should be a positive number.

    boost::uint_value_t::least
    

    The smallest, built-in, unsigned integral type that can hold all positive values up to and including V. The parameter should be a positive number.

提交回复
热议问题