title: How to print __int128 in g++?
body: <p>I am using the GCC built-in type <code>__int128</code> for a few things in my C++ program, nothing really significant, at least not enough to justify to use BigInt library only for that and, yet, enough to prevent to remove it totally.</p>
  
  <p>My problem comes when I run into the printing parts my classes, here is a minimal example:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main()
  {
    __int128 t = 1234567890;
  
    std::cout &lt;&lt; t &lt;&lt; std::endl;
  
    return t;
  }
  </code></pre>
  
  <p>Commenting out the <code>std::cout</code> line will make this code to compile nicely with <code>g++</code>, but having it will cause the following error message:</p>
  
  <pre><code>int128.c: In function ‘int main()’:
  int128.c:7:13: error: ambiguous overload for ‘operator&lt;&lt;’ (operand types are ‘std::ostream {aka std::basic_ostream&lt;char&gt;}’ and ‘__int128’)
     std::cout &lt;&lt; t &lt;&lt; std::endl;
               ^
  int128.c:7:13: note: candidates are:
  In file included from /usr/include/c++/4.9/iostream:39:0,
                   from int128.c:1:
  /usr/include/c++/4.9/ostream:108:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; (*)(std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp;)) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;] &lt;near match&gt;
         operator&lt;&lt;(__ostream_type&amp; (*__pf)(__ostream_type&amp;))
         ^
  /usr/include/c++/4.9/ostream:108:7: note:   no known conversion for argument 1 from ‘__int128’ to ‘std::basic_ostream&lt;char&gt;::__ostream_type&amp; (*)(std::basic_ostream&lt;char&gt;::__ostream_type&amp;) {aka std::basic_ostream&lt;char&gt;&amp; (*)(std::basic_ostream&lt;char&gt;&amp;)}’
  /usr/include/c++/4.9/ostream:117:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;::__ios_type&amp; (*)(std::basic_ostream&lt;_CharT, _Traits&gt;::__ios_type&amp;)) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ios_type = std::basic_ios&lt;char&gt;] &lt;near match&gt;
         operator&lt;&lt;(__ios_type&amp; (*__pf)(__ios_type&amp;))
         ^
  /usr/include/c++/4.9/ostream:117:7: note:   no known conversion for argument 1 from ‘__int128’ to ‘std::basic_ostream&lt;char&gt;::__ios_type&amp; (*)(std::basic_ostream&lt;char&gt;::__ios_type&amp;) {aka std::basic_ios&lt;char&gt;&amp; (*)(std::basic_ios&lt;char&gt;&amp;)}’
  /usr/include/c++/4.9/ostream:127:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(std::ios_base&amp; (*)(std::ios_base&amp;)) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;] &lt;near match&gt;
         operator&lt;&lt;(ios_base&amp; (*__pf) (ios_base&amp;))
         ^
  /usr/include/c++/4.9/ostream:127:7: note:   no known conversion for argument 1 from ‘__int128’ to ‘std::ios_base&amp; (*)(std::ios_base&amp;)’
  /usr/include/c++/4.9/ostream:166:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(long __n)
         ^
  /usr/include/c++/4.9/ostream:170:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long unsigned int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(unsigned long __n)
         ^
  /usr/include/c++/4.9/ostream:174:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(bool) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(bool __n)
         ^
  In file included from /usr/include/c++/4.9/ostream:609:0,
                   from /usr/include/c++/4.9/iostream:39,
                   from int128.c:1:
  /usr/include/c++/4.9/bits/ostream.tcc:91:5: note: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(short int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;]
       basic_ostream&lt;_CharT, _Traits&gt;::
       ^
  In file included from /usr/include/c++/4.9/iostream:39:0,
                   from int128.c:1:
  /usr/include/c++/4.9/ostream:181:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(short unsigned int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(unsigned short __n)
         ^
  In file included from /usr/include/c++/4.9/ostream:609:0,
                   from /usr/include/c++/4.9/iostream:39,
                   from int128.c:1:
  /usr/include/c++/4.9/bits/ostream.tcc:105:5: note: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;]
       basic_ostream&lt;_CharT, _Traits&gt;::
       ^
  In file included from /usr/include/c++/4.9/iostream:39:0,
                   from int128.c:1:
  /usr/include/c++/4.9/ostream:192:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(unsigned int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(unsigned int __n)
         ^
  /usr/include/c++/4.9/ostream:201:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long long int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(long long __n)
         ^
  /usr/include/c++/4.9/ostream:205:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long long unsigned int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(unsigned long long __n)
         ^
  /usr/include/c++/4.9/ostream:220:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(double) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(double __f)
         ^
  /usr/include/c++/4.9/ostream:224:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(float) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(float __f)
         ^
  /usr/include/c++/4.9/ostream:232:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long double) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(long double __f)
         ^
  /usr/include/c++/4.9/ostream:245:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(const void*) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;] &lt;near match&gt;
         operator&lt;&lt;(const void* __p)
         ^
  /usr/include/c++/4.9/ostream:245:7: note:   no known conversion for argument 1 from ‘__int128’ to ‘const void*’
  In file included from /usr/include/c++/4.9/ostream:609:0,
                   from /usr/include/c++/4.9/iostream:39,
                   from int128.c:1:
  /usr/include/c++/4.9/bits/ostream.tcc:119:5: note: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__streambuf_type = std::basic_streambuf&lt;char&gt;] &lt;near match&gt;
       basic_ostream&lt;_CharT, _Traits&gt;::
       ^
  /usr/include/c++/4.9/bits/ostream.tcc:119:5: note:   no known conversion for argument 1 from ‘__int128’ to ‘std::basic_ostream&lt;char&gt;::__streambuf_type* {aka std::basic_streambuf&lt;char&gt;*}’
  In file included from /usr/include/c++/4.9/iostream:39:0,
                   from int128.c:1:
  /usr/include/c++/4.9/ostream:493:5: note: std::basic_ostream&lt;char, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;char, _Traits&gt;&amp;, unsigned char) [with _Traits = std::char_traits&lt;char&gt;]
       operator&lt;&lt;(basic_ostream&lt;char, _Traits&gt;&amp; __out, unsigned char __c)
       ^
  /usr/include/c++/4.9/ostream:488:5: note: std::basic_ostream&lt;char, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;char, _Traits&gt;&amp;, signed char) [with _Traits = std::char_traits&lt;char&gt;]
       operator&lt;&lt;(basic_ostream&lt;char, _Traits&gt;&amp; __out, signed char __c)
       ^
  /usr/include/c++/4.9/ostream:482:5: note: std::basic_ostream&lt;char, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;char, _Traits&gt;&amp;, char) [with _Traits = std::char_traits&lt;char&gt;]
       operator&lt;&lt;(basic_ostream&lt;char, _Traits&gt;&amp; __out, char __c)
       ^
  /usr/include/c++/4.9/ostream:476:5: note: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;, char) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;]
       operator&lt;&lt;(basic_ostream&lt;_CharT, _Traits&gt;&amp; __out, char __c)
       ^
  </code></pre>
  
  <p>Yes, I know, a lot of lines to explain that <code>__int128</code> is just not properly handled...</p>
  
  <p>Is there a <strong>simple</strong> way to get <code>__int128</code> to be printed by the <code>iostream</code> as any other numeric types ?</p>
  
  <p>EDIT: For those who are still confusing C and C++, yes, I read the question: <a href="https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc">how to print __uint128_t number using gcc?</a> But, this was for C and not for C++ as I am asking now.</p>