[libcxx-commits] [libcxx] libcxx: std::ostream::sentry should be exported (PR #140169)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri May 16 00:15:14 PDT 2025


https://github.com/jeremyd2019 updated https://github.com/llvm/llvm-project/pull/140169

>From 41904dacef8c14213bb4184f31f9f17a4301740c Mon Sep 17 00:00:00 2001
From: Jeremy Drake <github at jdrake.com>
Date: Thu, 15 May 2025 17:58:31 -0700
Subject: [PATCH] libcxx: see if clang-cl likes this.

Not for review.
---
 libcxx/include/__ostream/basic_ostream.h | 46 +++++++++++-------------
 libcxx/include/istream                   | 45 +++++++++++------------
 2 files changed, 42 insertions(+), 49 deletions(-)

diff --git a/libcxx/include/__ostream/basic_ostream.h b/libcxx/include/__ostream/basic_ostream.h
index f7473a36d8ccc..924ffbb6bd80a 100644
--- a/libcxx/include/__ostream/basic_ostream.h
+++ b/libcxx/include/__ostream/basic_ostream.h
@@ -71,7 +71,7 @@ class basic_ostream : virtual public basic_ios<_CharT, _Traits> {
 
 public:
   // 27.7.2.4 Prefix/suffix:
-  class sentry;
+  class _LIBCPP_EXPORTED_FROM_ABI sentry;
 
   // 27.7.2.6 Formatted output:
   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&)) {
@@ -186,37 +186,33 @@ class basic_ostream<_CharT, _Traits>::sentry {
   basic_ostream<_CharT, _Traits>& __os_;
 
 public:
-  explicit sentry(basic_ostream<_CharT, _Traits>& __os);
-  ~sentry();
-  sentry(const sentry&)            = delete;
-  sentry& operator=(const sentry&) = delete;
-
-  _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; }
-};
-
-template <class _CharT, class _Traits>
-basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os) : __ok_(false), __os_(__os) {
-  if (__os.good()) {
-    if (__os.tie())
-      __os.tie()->flush();
-    __ok_ = true;
+  explicit inline sentry(basic_ostream<_CharT, _Traits>& __os) : __ok_(false), __os_(__os) {
+    if (__os.good()) {
+      if (__os.tie())
+        __os.tie()->flush();
+      __ok_ = true;
+    }
   }
-}
 
-template <class _CharT, class _Traits>
-basic_ostream<_CharT, _Traits>::sentry::~sentry() {
-  if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && uncaught_exceptions() == 0) {
+  inline ~sentry() {
+    if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && uncaught_exceptions() == 0) {
 #  if _LIBCPP_HAS_EXCEPTIONS
-    try {
+      try {
 #  endif // _LIBCPP_HAS_EXCEPTIONS
-      if (__os_.rdbuf()->pubsync() == -1)
-        __os_.setstate(ios_base::badbit);
+        if (__os_.rdbuf()->pubsync() == -1)
+          __os_.setstate(ios_base::badbit);
 #  if _LIBCPP_HAS_EXCEPTIONS
-    } catch (...) {
-    }
+      } catch (...) {
+      }
 #  endif // _LIBCPP_HAS_EXCEPTIONS
+    }
   }
-}
+
+  sentry(const sentry&)            = delete;
+  sentry& operator=(const sentry&) = delete;
+
+  _LIBCPP_HIDE_FROM_ABI explicit inline operator bool() const { return __ok_; }
+};
 
 template <class _CharT, class _Traits>
 basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs) {
diff --git a/libcxx/include/istream b/libcxx/include/istream
index 95340c739c118..d6bdd3464ec45 100644
--- a/libcxx/include/istream
+++ b/libcxx/include/istream
@@ -228,7 +228,7 @@ public:
   basic_istream& operator=(const basic_istream& __rhs) = delete;
 
   // 27.7.1.1.3 Prefix/suffix:
-  class sentry;
+  class _LIBCPP_EXPORTED_FROM_ABI sentry;
 
   // 27.7.1.2 Formatted input:
   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&)) {
@@ -309,36 +309,33 @@ class basic_istream<_CharT, _Traits>::sentry {
   bool __ok_;
 
 public:
-  explicit sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
+  explicit inline sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false) : __ok_(false) {
+    if (__is.good()) {
+      if (__is.tie())
+        __is.tie()->flush();
+      if (!__noskipws && (__is.flags() & ios_base::skipws)) {
+        typedef istreambuf_iterator<_CharT, _Traits> _Ip;
+        const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
+        _Ip __i(__is);
+        _Ip __eof;
+        for (; __i != __eof; ++__i)
+          if (!__ct.is(__ct.space, *__i))
+            break;
+        if (__i == __eof)
+          __is.setstate(ios_base::failbit | ios_base::eofbit);
+      }
+      __ok_ = __is.good();
+    } else
+      __is.setstate(ios_base::failbit);
+  }
   //    ~sentry() = default;
 
-  _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; }
+  _LIBCPP_HIDE_FROM_ABI explicit inline operator bool() const { return __ok_; }
 
   sentry(const sentry&)            = delete;
   sentry& operator=(const sentry&) = delete;
 };
 
-template <class _CharT, class _Traits>
-basic_istream<_CharT, _Traits>::sentry::sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws) : __ok_(false) {
-  if (__is.good()) {
-    if (__is.tie())
-      __is.tie()->flush();
-    if (!__noskipws && (__is.flags() & ios_base::skipws)) {
-      typedef istreambuf_iterator<_CharT, _Traits> _Ip;
-      const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
-      _Ip __i(__is);
-      _Ip __eof;
-      for (; __i != __eof; ++__i)
-        if (!__ct.is(__ct.space, *__i))
-          break;
-      if (__i == __eof)
-        __is.setstate(ios_base::failbit | ios_base::eofbit);
-    }
-    __ok_ = __is.good();
-  } else
-    __is.setstate(ios_base::failbit);
-}
-
 template <class _CharT, class _Traits>
 basic_istream<_CharT, _Traits>::basic_istream(basic_istream&& __rhs) : __gc_(__rhs.__gc_) {
   __rhs.__gc_ = 0;



More information about the libcxx-commits mailing list