I'm trying to format a boost multiprecision cpp_int using fmt library.
Here's the code I tried
using boost::multiprecision::cpp_int;
int main() {
cpp_int v("0x8FFFFFFFFFFFFFFFF");
std::string buffer;
fmt::format_to(std::back_inserter(buffer),"{}", v);
std::cout << buffer;
}
https://godbolt.org/z/M4zKc5hr6 using boost 1.73, fmt 9.1, gcc 7.3
I'm getting compilation error:
error: static assertion failed: Cannot format an argument. To make type T formattable provide a formatter specialization
I stumbled upon this issue https://github.com/fmtlib/fmt/issues/918 that seems to be resolved, so I was expecting fmt to be able to format boost multiprecision numbers. Do I still need to write a formatter specialization?
You need to provide a formatter. E.g. you can opt-in to the ostream formatter:
Live demo: Live On Coliru
Outputs:
BONUS: Hex, showbase, lower/uppercase
Demonstrating a simple custom formatter that invokes
i.str(...)
with the required formatting parameters:Live On Coliru
Prints