[libcxx-commits] [PATCH] D78816: Fix ostream for complex numbers with fixed field width

Antonio Sanchez via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Apr 30 14:32:57 PDT 2020


cantonios updated this revision to Diff 261353.
cantonios added a comment.
Herald added a subscriber: broadwaylamb.

Updated with test.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D78816/new/

https://reviews.llvm.org/D78816

Files:
  libcxx/include/complex
  libcxx/test/std/numerics/complex.number/complex.ops/stream_output.pass.cpp


Index: libcxx/test/std/numerics/complex.number/complex.ops/stream_output.pass.cpp
===================================================================
--- libcxx/test/std/numerics/complex.number/complex.ops/stream_output.pass.cpp
+++ libcxx/test/std/numerics/complex.number/complex.ops/stream_output.pass.cpp
@@ -20,10 +20,20 @@
 
 int main(int, char**)
 {
-    std::complex<double> c(1, 2);
-    std::ostringstream os;
-    os << c;
-    assert(os.str() == "(1,2)");
+    {
+        std::complex<double> c(1, 2);
+        std::ostringstream os;
+        os << c;
+        assert(os.str() == "(1,2)");
+    }
+    {
+        std::complex<double> c(1, 2);
+        std::ostringstream os;
+        os.width(8);
+        os.fill('_');
+        os << c;
+        assert(os.str() == "___(1,2)");
+    }
 
   return 0;
 }
Index: libcxx/include/complex
===================================================================
--- libcxx/include/complex
+++ libcxx/include/complex
@@ -244,6 +244,7 @@
 #include <stdexcept>
 #include <cmath>
 #include <iosfwd>
+#include <sstream>
 #include <version>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -1441,7 +1442,12 @@
 basic_ostream<_CharT, _Traits>&
 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
 {
-    return __os << '(' << __x.real() << ',' << __x.imag() << ')';
+    basic_ostringstream<_CharT, _Traits> __s;
+    __s.flags(__os.flags());
+    __s.imbue(__os.getloc());
+    __s.precision(__os.precision());
+    __s << '(' << __x.real() << ',' << __x.imag() << ')';
+    return __os << __s.str();
 }
 
 #if _LIBCPP_STD_VER > 11


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78816.261353.patch
Type: text/x-patch
Size: 1607 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200430/c11db08e/attachment-0001.bin>


More information about the libcxx-commits mailing list