Since dynamic allocation is not allowed during compile-time, you can only have a type that's arbitrarily long at run-time. That's why standard types like std::string
or std::vector
doesn't have any constexpr constructors. The same applies to arbitrary-precision math. A workaround is to declare the variable as static
so that it's computed only once at startup
However if you know the range of your values then you can use ctbignum which is a Library for Multiprecision Compile-Time and Run-Time Arithmetic (including Modular Arithmetic)
This is a header-only template library for fixed-width "small big-integer" computations, for use during run-time as well as compile-time. By "small big integers", we mean numbers with a few limbs (in other words, a few hundred bits), typically occurring in cryptographic applications.
Boost.MPL may be another choice in when the limits are known
In C++20 some kind of dynamic allocation in constexpr functions is allowed, so there may be a real compile-time bignum library then
But I don't think arbitrary-precision would be the solution for solving fixed-point arithmetic. That completely defeats the purpose and you can simply use a floating-point right from the beginning. So this is likely an XY problem and you should ask how to do those fixed-point operations instead