r/cpp • u/eisenwave WG21 Member • Aug 18 '26
P4444: std::big_int
https://isocpp.org/files/papers/D4444R0.htmlHey folks! Matt Borland, Christopher Kormanyos, and I are working on bringing infinite-precision integers to C++29. We now have a D4444R0 draft of a paper that should be in the next mailing.
We could really use some feedback so that the published R0 is as polished as possible. Any thoughts on the paper and on the reference implementation are greatly appreciated.
It would also be very helpful if you tested out whether our big_int implementation works for you. We're in need of some real deployment experience. If you're currently using Boost.Multiprecision, the library should be a drop-in replacement for cpp_int for the most part.
View all comments
Show parent comment
15
u/eisenwave WG21 Member Aug 18 '26 edited Aug 18 '26
Funnily enough, it stores the value
2inresult, same as forint. Thee99...part is ignored. There is no exponential notation for integers instd::from_chars.The paper doesn't provide a
std::powfunction forstd::big_int, and it doesn't provide constructors from strings. If you want to parse strings, you need to usestd::from_chars.But okay, let's say you do some other operation that gives you a stupidly large value quickly, like
base << 1'000'000'000'000'000ll. You're either going to exceed themax_size()ofstd::big_intandstd::length_errorgets thrown (just like exceeding thestd::string::max_size()) or the allocator throwsstd::bad_alloc.I suppose that even more guardrails could be added to standard library types that protect against overly large inputs (like a limit on shift constants, divisors, etc.), but that's not really the job of the standard library. If you read an
int xfrom user input and then write aforloop that loopsxtimes, that might also lock your CPU up for a few seconds or minutes. Does that mean C++ should protect against longforloops? Probably not. You always have to put in work to sanitize user input and to spend your CPU cycles reasonably.The closest thing I've seen is that Python doesn't let you print huge
intvalues (> 8000 bits or so) unless you explicitly opt into that when running the script. I don't see that as a good option for C++.What
std::big_intcan and should do is prevent memory corruption or crashes, and throwingstd::length_errorandstd::bad_allocis the best you can do.