How to print __int128 in g++?

前端 未结 6 1756
再見小時候
再見小時候 2020-12-05 06:48

I am using the GCC built-in type __int128 for a few things in my C++ program, nothing really significant, at least not enough to justify to use BigInt library o

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 07:08

    The stock cout does not handle __int128, but you may extends it with your own function.

    For starter, code something like this:

    std::ostream& operator<<(std::ostream& os, __int128 t) {
        // TODO: Convert t to string
        return os << str;
    }
    

    There are many solution on SO to convert 128 bit number to string, I'll not repeat here.

    About library compatibility in comment:

    You only need to roll your own function if the standard library does not provide such handler. Once the library support the type, you should then see a conflict when building, something like [ note: built-in candidate operator<< ], go try that with int64_t.

提交回复
热议问题