简体   繁体   English

Output 128 位 integer 使用 stream 运算符

[英]Output 128 bit integer using stream operator

I'm using GCC's 128 bit integer:我正在使用 GCC 的 128 位 integer:

__extension__ using uint128_t = unsigned __int128;
uint128_t a = 545;
std::cout << a << std::endl;

However, if I try to output using the stream operator I get the compiler error:但是,如果我尝试使用 stream 运算符访问 output,则会出现编译器错误:

error: ambiguous overload for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘uint128_t’ {aka ‘__int128 unsigned’})

Is there a way to allow this?有没有办法允许这样做?

Linux, GCC version 11.1, x86-64 Linux、GCC 版本 11.1、x86-64

libstdc++ does not have an ostream overload for __int128 . libstdc++ 没有__int128ostream重载。 However, you can use C++20 <format> library, which supports __int128 formatting in both libstdc++ and libc++.但是,您可以使用 C++20 <format>库,它在 libstdc++ 和 libc++ 中都支持__int128格式。

#include <format>
#include <iostream>

int main() {
  __extension__ using uint128_t = unsigned __int128;
  uint128_t a = 545;
  std::cout << std::format("{}\n", a);
}

Demo演示

You will have to overload the << operator yourself because std::ostream doesn't have an overload for that type as it is from an external lib.您必须自己重载<<运算符,因为std::ostream没有该类型的重载,因为它来自外部库。 This could help. 可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM