ostream 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_OSTREAM
  10. #define _LIBCPP_OSTREAM
  11. /*
  12. ostream synopsis
  13. template <class charT, class traits = char_traits<charT> >
  14. class basic_ostream
  15. : virtual public basic_ios<charT,traits>
  16. {
  17. public:
  18. // types (inherited from basic_ios (27.5.4)):
  19. typedef charT char_type;
  20. typedef traits traits_type;
  21. typedef typename traits_type::int_type int_type;
  22. typedef typename traits_type::pos_type pos_type;
  23. typedef typename traits_type::off_type off_type;
  24. // 27.7.2.2 Constructor/destructor:
  25. explicit basic_ostream(basic_streambuf<char_type,traits>* sb);
  26. basic_ostream(basic_ostream&& rhs);
  27. virtual ~basic_ostream();
  28. // 27.7.2.3 Assign/swap
  29. basic_ostream& operator=(const basic_ostream& rhs) = delete; // C++14
  30. basic_ostream& operator=(basic_ostream&& rhs);
  31. void swap(basic_ostream& rhs);
  32. // 27.7.2.4 Prefix/suffix:
  33. class sentry;
  34. // 27.7.2.6 Formatted output:
  35. basic_ostream& operator<<(basic_ostream& (*pf)(basic_ostream&));
  36. basic_ostream& operator<<(basic_ios<charT, traits>& (*pf)(basic_ios<charT,traits>&));
  37. basic_ostream& operator<<(ios_base& (*pf)(ios_base&));
  38. basic_ostream& operator<<(bool n);
  39. basic_ostream& operator<<(short n);
  40. basic_ostream& operator<<(unsigned short n);
  41. basic_ostream& operator<<(int n);
  42. basic_ostream& operator<<(unsigned int n);
  43. basic_ostream& operator<<(long n);
  44. basic_ostream& operator<<(unsigned long n);
  45. basic_ostream& operator<<(long long n);
  46. basic_ostream& operator<<(unsigned long long n);
  47. basic_ostream& operator<<(float f);
  48. basic_ostream& operator<<(double f);
  49. basic_ostream& operator<<(long double f);
  50. basic_ostream& operator<<(const void* p);
  51. basic_ostream& operator<<(const volatile void* val); // C++23
  52. basic_ostream& operator<<(basic_streambuf<char_type,traits>* sb);
  53. basic_ostream& operator<<(nullptr_t);
  54. // 27.7.2.7 Unformatted output:
  55. basic_ostream& put(char_type c);
  56. basic_ostream& write(const char_type* s, streamsize n);
  57. basic_ostream& flush();
  58. // 27.7.2.5 seeks:
  59. pos_type tellp();
  60. basic_ostream& seekp(pos_type);
  61. basic_ostream& seekp(off_type, ios_base::seekdir);
  62. protected:
  63. basic_ostream(const basic_ostream& rhs) = delete;
  64. basic_ostream(basic_ostream&& rhs);
  65. // 27.7.3.3 Assign/swap
  66. basic_ostream& operator=(basic_ostream& rhs) = delete;
  67. basic_ostream& operator=(const basic_ostream&& rhs);
  68. void swap(basic_ostream& rhs);
  69. };
  70. // 27.7.2.6.4 character inserters
  71. template<class charT, class traits>
  72. basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, charT);
  73. template<class charT, class traits>
  74. basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, char);
  75. template<class traits>
  76. basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, char);
  77. // signed and unsigned
  78. template<class traits>
  79. basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, signed char);
  80. template<class traits>
  81. basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, unsigned char);
  82. // NTBS
  83. template<class charT, class traits>
  84. basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const charT*);
  85. template<class charT, class traits>
  86. basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const char*);
  87. template<class traits>
  88. basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const char*);
  89. // signed and unsigned
  90. template<class traits>
  91. basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const signed char*);
  92. template<class traits>
  93. basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const unsigned char*);
  94. // swap:
  95. template <class charT, class traits>
  96. void swap(basic_ostream<charT, traits>& x, basic_ostream<charT, traits>& y);
  97. template <class charT, class traits>
  98. basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
  99. template <class charT, class traits>
  100. basic_ostream<charT,traits>& ends(basic_ostream<charT,traits>& os);
  101. template <class charT, class traits>
  102. basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os);
  103. // rvalue stream insertion
  104. template <class Stream, class T>
  105. Stream&& operator<<(Stream&& os, const T& x);
  106. template<class traits>
  107. basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, wchar_t) = delete; // since C++20
  108. template<class traits>
  109. basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char8_t) = delete; // since C++20
  110. template<class traits>
  111. basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char16_t) = delete; // since C++20
  112. template<class traits>
  113. basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char32_t) = delete; // since C++20
  114. template<class traits>
  115. basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, char8_t) = delete; // since C++20
  116. template<class traits>
  117. basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, char16_t) = delete; // since C++20
  118. template<class traits>
  119. basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, char32_t) = delete; // since C++20
  120. template<class traits>
  121. basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const wchar_t*) = delete; // since C++20
  122. template<class traits>
  123. basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const char8_t*) = delete; // since C++20
  124. template<class traits>
  125. basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const char16_t*) = delete; // since C++20
  126. template<class traits>
  127. basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const char32_t*) = delete; // since C++20
  128. template<class traits>
  129. basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, const char8_t*) = delete; // since C++20
  130. template<class traits>
  131. basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, const char16_t*) = delete; // since C++20
  132. template<class traits>
  133. basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, const char32_t*) = delete; // since C++20
  134. // [ostream.formatted.print], print functions
  135. template<class... Args> // since C++23
  136. void print(ostream& os, format_string<Args...> fmt, Args&&... args);
  137. template<class... Args> // since C++23
  138. void println(ostream& os, format_string<Args...> fmt, Args&&... args);
  139. void vprint_unicode(ostream& os, string_view fmt, format_args args); // since C++23
  140. void vprint_nonunicode(ostream& os, string_view fmt, format_args args); // since C++23
  141. } // std
  142. */
  143. #include <__availability>
  144. #include <__config>
  145. #include <__exception/operations.h>
  146. #include <__fwd/ostream.h>
  147. #include <__memory/shared_ptr.h>
  148. #include <__memory/unique_ptr.h>
  149. #include <__system_error/error_code.h>
  150. #include <__type_traits/conjunction.h>
  151. #include <__type_traits/enable_if.h>
  152. #include <__type_traits/is_base_of.h>
  153. #include <__type_traits/void_t.h>
  154. #include <__utility/declval.h>
  155. #include <bitset>
  156. #include <cstdio>
  157. #include <format>
  158. #include <ios>
  159. #include <locale>
  160. #include <new>
  161. #include <print>
  162. #include <streambuf>
  163. #include <string_view>
  164. #include <version>
  165. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  166. # pragma GCC system_header
  167. #endif
  168. _LIBCPP_PUSH_MACROS
  169. #include <__undef_macros>
  170. _LIBCPP_BEGIN_NAMESPACE_STD
  171. template <class _CharT, class _Traits>
  172. class _LIBCPP_TEMPLATE_VIS basic_ostream : virtual public basic_ios<_CharT, _Traits> {
  173. public:
  174. // types (inherited from basic_ios (27.5.4)):
  175. typedef _CharT char_type;
  176. typedef _Traits traits_type;
  177. typedef typename traits_type::int_type int_type;
  178. typedef typename traits_type::pos_type pos_type;
  179. typedef typename traits_type::off_type off_type;
  180. // 27.7.2.2 Constructor/destructor:
  181. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb) {
  182. this->init(__sb);
  183. }
  184. ~basic_ostream() override;
  185. protected:
  186. inline _LIBCPP_HIDE_FROM_ABI basic_ostream(basic_ostream&& __rhs);
  187. // 27.7.2.3 Assign/swap
  188. inline _LIBCPP_HIDE_FROM_ABI basic_ostream& operator=(basic_ostream&& __rhs);
  189. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_ostream& __rhs) {
  190. basic_ios<char_type, traits_type>::swap(__rhs);
  191. }
  192. basic_ostream(const basic_ostream& __rhs) = delete;
  193. basic_ostream& operator=(const basic_ostream& __rhs) = delete;
  194. public:
  195. // 27.7.2.4 Prefix/suffix:
  196. class _LIBCPP_TEMPLATE_VIS sentry;
  197. // 27.7.2.6 Formatted output:
  198. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&)) {
  199. return __pf(*this);
  200. }
  201. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream&
  202. operator<<(basic_ios<char_type, traits_type>& (*__pf)(basic_ios<char_type, traits_type>&)) {
  203. __pf(*this);
  204. return *this;
  205. }
  206. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(ios_base& (*__pf)(ios_base&)) {
  207. __pf(*this);
  208. return *this;
  209. }
  210. basic_ostream& operator<<(bool __n);
  211. basic_ostream& operator<<(short __n);
  212. basic_ostream& operator<<(unsigned short __n);
  213. basic_ostream& operator<<(int __n);
  214. basic_ostream& operator<<(unsigned int __n);
  215. basic_ostream& operator<<(long __n);
  216. basic_ostream& operator<<(unsigned long __n);
  217. basic_ostream& operator<<(long long __n);
  218. basic_ostream& operator<<(unsigned long long __n);
  219. basic_ostream& operator<<(float __f);
  220. basic_ostream& operator<<(double __f);
  221. basic_ostream& operator<<(long double __f);
  222. basic_ostream& operator<<(const void* __p);
  223. #if _LIBCPP_STD_VER >= 23
  224. _LIBCPP_HIDE_FROM_ABI basic_ostream& operator<<(const volatile void* __p) {
  225. return operator<<(const_cast<const void*>(__p));
  226. }
  227. #endif
  228. basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
  229. #if _LIBCPP_STD_VER >= 17
  230. // LWG 2221 - nullptr. This is not backported to older standards modes.
  231. // See https://reviews.llvm.org/D127033 for more info on the rationale.
  232. _LIBCPP_HIDE_FROM_ABI basic_ostream& operator<<(nullptr_t) { return *this << "nullptr"; }
  233. #endif
  234. // 27.7.2.7 Unformatted output:
  235. basic_ostream& put(char_type __c);
  236. basic_ostream& write(const char_type* __s, streamsize __n);
  237. basic_ostream& flush();
  238. // 27.7.2.5 seeks:
  239. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type tellp();
  240. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(pos_type __pos);
  241. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
  242. protected:
  243. _LIBCPP_HIDE_FROM_ABI basic_ostream() {} // extension, intentially does not initialize
  244. };
  245. template <class _CharT, class _Traits>
  246. class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry {
  247. bool __ok_;
  248. basic_ostream<_CharT, _Traits>& __os_;
  249. public:
  250. explicit sentry(basic_ostream<_CharT, _Traits>& __os);
  251. ~sentry();
  252. sentry(const sentry&) = delete;
  253. sentry& operator=(const sentry&) = delete;
  254. _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; }
  255. };
  256. template <class _CharT, class _Traits>
  257. basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os) : __ok_(false), __os_(__os) {
  258. if (__os.good()) {
  259. if (__os.tie())
  260. __os.tie()->flush();
  261. __ok_ = true;
  262. }
  263. }
  264. template <class _CharT, class _Traits>
  265. basic_ostream<_CharT, _Traits>::sentry::~sentry() {
  266. if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && !uncaught_exception()) {
  267. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  268. try {
  269. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  270. if (__os_.rdbuf()->pubsync() == -1)
  271. __os_.setstate(ios_base::badbit);
  272. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  273. } catch (...) {
  274. }
  275. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  276. }
  277. }
  278. template <class _CharT, class _Traits>
  279. basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs) {
  280. this->move(__rhs);
  281. }
  282. template <class _CharT, class _Traits>
  283. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs) {
  284. swap(__rhs);
  285. return *this;
  286. }
  287. template <class _CharT, class _Traits>
  288. basic_ostream<_CharT, _Traits>::~basic_ostream() {}
  289. template <class _CharT, class _Traits>
  290. basic_ostream<_CharT, _Traits>&
  291. basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb) {
  292. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  293. try {
  294. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  295. sentry __s(*this);
  296. if (__s) {
  297. if (__sb) {
  298. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  299. try {
  300. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  301. typedef istreambuf_iterator<_CharT, _Traits> _Ip;
  302. typedef ostreambuf_iterator<_CharT, _Traits> _Op;
  303. _Ip __i(__sb);
  304. _Ip __eof;
  305. _Op __o(*this);
  306. size_t __c = 0;
  307. for (; __i != __eof; ++__i, ++__o, ++__c) {
  308. *__o = *__i;
  309. if (__o.failed())
  310. break;
  311. }
  312. if (__c == 0)
  313. this->setstate(ios_base::failbit);
  314. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  315. } catch (...) {
  316. this->__set_failbit_and_consider_rethrow();
  317. }
  318. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  319. } else
  320. this->setstate(ios_base::badbit);
  321. }
  322. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  323. } catch (...) {
  324. this->__set_badbit_and_consider_rethrow();
  325. }
  326. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  327. return *this;
  328. }
  329. template <class _CharT, class _Traits>
  330. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(bool __n) {
  331. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  332. try {
  333. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  334. sentry __s(*this);
  335. if (__s) {
  336. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  337. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  338. if (__f.put(*this, *this, this->fill(), __n).failed())
  339. this->setstate(ios_base::badbit | ios_base::failbit);
  340. }
  341. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  342. } catch (...) {
  343. this->__set_badbit_and_consider_rethrow();
  344. }
  345. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  346. return *this;
  347. }
  348. template <class _CharT, class _Traits>
  349. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(short __n) {
  350. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  351. try {
  352. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  353. sentry __s(*this);
  354. if (__s) {
  355. ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
  356. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  357. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  358. if (__f.put(*this,
  359. *this,
  360. this->fill(),
  361. __flags == ios_base::oct || __flags == ios_base::hex
  362. ? static_cast<long>(static_cast<unsigned short>(__n))
  363. : static_cast<long>(__n))
  364. .failed())
  365. this->setstate(ios_base::badbit | ios_base::failbit);
  366. }
  367. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  368. } catch (...) {
  369. this->__set_badbit_and_consider_rethrow();
  370. }
  371. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  372. return *this;
  373. }
  374. template <class _CharT, class _Traits>
  375. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n) {
  376. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  377. try {
  378. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  379. sentry __s(*this);
  380. if (__s) {
  381. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  382. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  383. if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
  384. this->setstate(ios_base::badbit | ios_base::failbit);
  385. }
  386. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  387. } catch (...) {
  388. this->__set_badbit_and_consider_rethrow();
  389. }
  390. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  391. return *this;
  392. }
  393. template <class _CharT, class _Traits>
  394. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(int __n) {
  395. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  396. try {
  397. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  398. sentry __s(*this);
  399. if (__s) {
  400. ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
  401. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  402. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  403. if (__f.put(*this,
  404. *this,
  405. this->fill(),
  406. __flags == ios_base::oct || __flags == ios_base::hex
  407. ? static_cast<long>(static_cast<unsigned int>(__n))
  408. : static_cast<long>(__n))
  409. .failed())
  410. this->setstate(ios_base::badbit | ios_base::failbit);
  411. }
  412. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  413. } catch (...) {
  414. this->__set_badbit_and_consider_rethrow();
  415. }
  416. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  417. return *this;
  418. }
  419. template <class _CharT, class _Traits>
  420. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n) {
  421. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  422. try {
  423. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  424. sentry __s(*this);
  425. if (__s) {
  426. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  427. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  428. if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
  429. this->setstate(ios_base::badbit | ios_base::failbit);
  430. }
  431. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  432. } catch (...) {
  433. this->__set_badbit_and_consider_rethrow();
  434. }
  435. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  436. return *this;
  437. }
  438. template <class _CharT, class _Traits>
  439. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __n) {
  440. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  441. try {
  442. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  443. sentry __s(*this);
  444. if (__s) {
  445. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  446. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  447. if (__f.put(*this, *this, this->fill(), __n).failed())
  448. this->setstate(ios_base::badbit | ios_base::failbit);
  449. }
  450. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  451. } catch (...) {
  452. this->__set_badbit_and_consider_rethrow();
  453. }
  454. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  455. return *this;
  456. }
  457. template <class _CharT, class _Traits>
  458. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n) {
  459. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  460. try {
  461. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  462. sentry __s(*this);
  463. if (__s) {
  464. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  465. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  466. if (__f.put(*this, *this, this->fill(), __n).failed())
  467. this->setstate(ios_base::badbit | ios_base::failbit);
  468. }
  469. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  470. } catch (...) {
  471. this->__set_badbit_and_consider_rethrow();
  472. }
  473. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  474. return *this;
  475. }
  476. template <class _CharT, class _Traits>
  477. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long long __n) {
  478. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  479. try {
  480. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  481. sentry __s(*this);
  482. if (__s) {
  483. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  484. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  485. if (__f.put(*this, *this, this->fill(), __n).failed())
  486. this->setstate(ios_base::badbit | ios_base::failbit);
  487. }
  488. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  489. } catch (...) {
  490. this->__set_badbit_and_consider_rethrow();
  491. }
  492. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  493. return *this;
  494. }
  495. template <class _CharT, class _Traits>
  496. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n) {
  497. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  498. try {
  499. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  500. sentry __s(*this);
  501. if (__s) {
  502. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  503. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  504. if (__f.put(*this, *this, this->fill(), __n).failed())
  505. this->setstate(ios_base::badbit | ios_base::failbit);
  506. }
  507. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  508. } catch (...) {
  509. this->__set_badbit_and_consider_rethrow();
  510. }
  511. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  512. return *this;
  513. }
  514. template <class _CharT, class _Traits>
  515. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(float __n) {
  516. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  517. try {
  518. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  519. sentry __s(*this);
  520. if (__s) {
  521. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  522. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  523. if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())
  524. this->setstate(ios_base::badbit | ios_base::failbit);
  525. }
  526. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  527. } catch (...) {
  528. this->__set_badbit_and_consider_rethrow();
  529. }
  530. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  531. return *this;
  532. }
  533. template <class _CharT, class _Traits>
  534. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(double __n) {
  535. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  536. try {
  537. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  538. sentry __s(*this);
  539. if (__s) {
  540. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  541. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  542. if (__f.put(*this, *this, this->fill(), __n).failed())
  543. this->setstate(ios_base::badbit | ios_base::failbit);
  544. }
  545. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  546. } catch (...) {
  547. this->__set_badbit_and_consider_rethrow();
  548. }
  549. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  550. return *this;
  551. }
  552. template <class _CharT, class _Traits>
  553. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long double __n) {
  554. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  555. try {
  556. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  557. sentry __s(*this);
  558. if (__s) {
  559. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  560. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  561. if (__f.put(*this, *this, this->fill(), __n).failed())
  562. this->setstate(ios_base::badbit | ios_base::failbit);
  563. }
  564. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  565. } catch (...) {
  566. this->__set_badbit_and_consider_rethrow();
  567. }
  568. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  569. return *this;
  570. }
  571. template <class _CharT, class _Traits>
  572. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const void* __n) {
  573. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  574. try {
  575. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  576. sentry __s(*this);
  577. if (__s) {
  578. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  579. const _Fp& __f = std::use_facet<_Fp>(this->getloc());
  580. if (__f.put(*this, *this, this->fill(), __n).failed())
  581. this->setstate(ios_base::badbit | ios_base::failbit);
  582. }
  583. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  584. } catch (...) {
  585. this->__set_badbit_and_consider_rethrow();
  586. }
  587. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  588. return *this;
  589. }
  590. template <class _CharT, class _Traits>
  591. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  592. __put_character_sequence(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str, size_t __len) {
  593. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  594. try {
  595. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  596. typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
  597. if (__s) {
  598. typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
  599. if (std::__pad_and_output(
  600. _Ip(__os),
  601. __str,
  602. (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
  603. __str + __len,
  604. __os,
  605. __os.fill())
  606. .failed())
  607. __os.setstate(ios_base::badbit | ios_base::failbit);
  608. }
  609. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  610. } catch (...) {
  611. __os.__set_badbit_and_consider_rethrow();
  612. }
  613. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  614. return __os;
  615. }
  616. template <class _CharT, class _Traits>
  617. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) {
  618. return std::__put_character_sequence(__os, &__c, 1);
  619. }
  620. template <class _CharT, class _Traits>
  621. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn) {
  622. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  623. try {
  624. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  625. typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
  626. if (__s) {
  627. _CharT __c = __os.widen(__cn);
  628. typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
  629. if (std::__pad_and_output(
  630. _Ip(__os),
  631. &__c,
  632. (__os.flags() & ios_base::adjustfield) == ios_base::left ? &__c + 1 : &__c,
  633. &__c + 1,
  634. __os,
  635. __os.fill())
  636. .failed())
  637. __os.setstate(ios_base::badbit | ios_base::failbit);
  638. }
  639. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  640. } catch (...) {
  641. __os.__set_badbit_and_consider_rethrow();
  642. }
  643. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  644. return __os;
  645. }
  646. template <class _Traits>
  647. _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, char __c) {
  648. return std::__put_character_sequence(__os, &__c, 1);
  649. }
  650. template <class _Traits>
  651. _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, signed char __c) {
  652. return std::__put_character_sequence(__os, (char*)&__c, 1);
  653. }
  654. template <class _Traits>
  655. _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c) {
  656. return std::__put_character_sequence(__os, (char*)&__c, 1);
  657. }
  658. template <class _CharT, class _Traits>
  659. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  660. operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str) {
  661. return std::__put_character_sequence(__os, __str, _Traits::length(__str));
  662. }
  663. template <class _CharT, class _Traits>
  664. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  665. operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn) {
  666. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  667. try {
  668. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  669. typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
  670. if (__s) {
  671. typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
  672. size_t __len = char_traits<char>::length(__strn);
  673. const int __bs = 100;
  674. _CharT __wbb[__bs];
  675. _CharT* __wb = __wbb;
  676. unique_ptr<_CharT, void (*)(void*)> __h(0, free);
  677. if (__len > __bs) {
  678. __wb = (_CharT*)malloc(__len * sizeof(_CharT));
  679. if (__wb == 0)
  680. __throw_bad_alloc();
  681. __h.reset(__wb);
  682. }
  683. for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p)
  684. *__p = __os.widen(*__strn);
  685. if (std::__pad_and_output(
  686. _Ip(__os),
  687. __wb,
  688. (__os.flags() & ios_base::adjustfield) == ios_base::left ? __wb + __len : __wb,
  689. __wb + __len,
  690. __os,
  691. __os.fill())
  692. .failed())
  693. __os.setstate(ios_base::badbit | ios_base::failbit);
  694. }
  695. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  696. } catch (...) {
  697. __os.__set_badbit_and_consider_rethrow();
  698. }
  699. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  700. return __os;
  701. }
  702. template <class _Traits>
  703. _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, const char* __str) {
  704. return std::__put_character_sequence(__os, __str, _Traits::length(__str));
  705. }
  706. template <class _Traits>
  707. _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
  708. operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str) {
  709. const char* __s = (const char*)__str;
  710. return std::__put_character_sequence(__os, __s, _Traits::length(__s));
  711. }
  712. template <class _Traits>
  713. _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
  714. operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str) {
  715. const char* __s = (const char*)__str;
  716. return std::__put_character_sequence(__os, __s, _Traits::length(__s));
  717. }
  718. template <class _CharT, class _Traits>
  719. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::put(char_type __c) {
  720. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  721. try {
  722. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  723. sentry __s(*this);
  724. if (__s) {
  725. typedef ostreambuf_iterator<_CharT, _Traits> _Op;
  726. _Op __o(*this);
  727. *__o = __c;
  728. if (__o.failed())
  729. this->setstate(ios_base::badbit);
  730. }
  731. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  732. } catch (...) {
  733. this->__set_badbit_and_consider_rethrow();
  734. }
  735. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  736. return *this;
  737. }
  738. template <class _CharT, class _Traits>
  739. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n) {
  740. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  741. try {
  742. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  743. sentry __sen(*this);
  744. if (__sen && __n) {
  745. if (this->rdbuf()->sputn(__s, __n) != __n)
  746. this->setstate(ios_base::badbit);
  747. }
  748. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  749. } catch (...) {
  750. this->__set_badbit_and_consider_rethrow();
  751. }
  752. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  753. return *this;
  754. }
  755. template <class _CharT, class _Traits>
  756. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::flush() {
  757. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  758. try {
  759. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  760. if (this->rdbuf()) {
  761. sentry __s(*this);
  762. if (__s) {
  763. if (this->rdbuf()->pubsync() == -1)
  764. this->setstate(ios_base::badbit);
  765. }
  766. }
  767. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  768. } catch (...) {
  769. this->__set_badbit_and_consider_rethrow();
  770. }
  771. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  772. return *this;
  773. }
  774. template <class _CharT, class _Traits>
  775. typename basic_ostream<_CharT, _Traits>::pos_type basic_ostream<_CharT, _Traits>::tellp() {
  776. if (this->fail())
  777. return pos_type(-1);
  778. return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
  779. }
  780. template <class _CharT, class _Traits>
  781. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(pos_type __pos) {
  782. sentry __s(*this);
  783. if (!this->fail()) {
  784. if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1))
  785. this->setstate(ios_base::failbit);
  786. }
  787. return *this;
  788. }
  789. template <class _CharT, class _Traits>
  790. basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir) {
  791. sentry __s(*this);
  792. if (!this->fail()) {
  793. if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))
  794. this->setstate(ios_base::failbit);
  795. }
  796. return *this;
  797. }
  798. template <class _CharT, class _Traits>
  799. _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& endl(basic_ostream<_CharT, _Traits>& __os) {
  800. __os.put(__os.widen('\n'));
  801. __os.flush();
  802. return __os;
  803. }
  804. template <class _CharT, class _Traits>
  805. _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& ends(basic_ostream<_CharT, _Traits>& __os) {
  806. __os.put(_CharT());
  807. return __os;
  808. }
  809. template <class _CharT, class _Traits>
  810. _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& flush(basic_ostream<_CharT, _Traits>& __os) {
  811. __os.flush();
  812. return __os;
  813. }
  814. template <class _Stream, class _Tp, class = void>
  815. struct __is_ostreamable : false_type {};
  816. template <class _Stream, class _Tp>
  817. struct __is_ostreamable<_Stream, _Tp, decltype(std::declval<_Stream>() << std::declval<_Tp>(), void())> : true_type {};
  818. template <class _Stream,
  819. class _Tp,
  820. __enable_if_t<_And<is_base_of<ios_base, _Stream>, __is_ostreamable<_Stream&, const _Tp&> >::value, int> = 0>
  821. _LIBCPP_HIDE_FROM_ABI _Stream&& operator<<(_Stream&& __os, const _Tp& __x) {
  822. __os << __x;
  823. return std::move(__os);
  824. }
  825. template <class _CharT, class _Traits, class _Allocator>
  826. basic_ostream<_CharT, _Traits>&
  827. operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str) {
  828. return std::__put_character_sequence(__os, __str.data(), __str.size());
  829. }
  830. template <class _CharT, class _Traits>
  831. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  832. operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv) {
  833. return std::__put_character_sequence(__os, __sv.data(), __sv.size());
  834. }
  835. template <class _CharT, class _Traits>
  836. inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  837. operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec) {
  838. return __os << __ec.category().name() << ':' << __ec.value();
  839. }
  840. template <class _CharT, class _Traits, class _Yp>
  841. inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  842. operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p) {
  843. return __os << __p.get();
  844. }
  845. template <
  846. class _CharT,
  847. class _Traits,
  848. class _Yp,
  849. class _Dp,
  850. __enable_if_t<is_same<void,
  851. __void_t<decltype((std::declval<basic_ostream<_CharT, _Traits>&>()
  852. << std::declval<typename unique_ptr<_Yp, _Dp>::pointer>()))> >::value,
  853. int> = 0>
  854. inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  855. operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p) {
  856. return __os << __p.get();
  857. }
  858. template <class _CharT, class _Traits, size_t _Size>
  859. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  860. operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x) {
  861. return __os << __x.template to_string<_CharT, _Traits>(std::use_facet<ctype<_CharT> >(__os.getloc()).widen('0'),
  862. std::use_facet<ctype<_CharT> >(__os.getloc()).widen('1'));
  863. }
  864. #if 0
  865. # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  866. template <class _Traits>
  867. basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete;
  868. template <class _Traits>
  869. basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete;
  870. template <class _Traits>
  871. basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete;
  872. template <class _Traits>
  873. basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete;
  874. template <class _Traits>
  875. basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete;
  876. template <class _Traits>
  877. basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete;
  878. # endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
  879. # ifndef _LIBCPP_HAS_NO_CHAR8_T
  880. template <class _Traits>
  881. basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char8_t) = delete;
  882. template <class _Traits>
  883. basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete;
  884. template <class _Traits>
  885. basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete;
  886. template <class _Traits>
  887. basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete;
  888. # endif
  889. template <class _Traits>
  890. basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char16_t) = delete;
  891. template <class _Traits>
  892. basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char32_t) = delete;
  893. template <class _Traits>
  894. basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete;
  895. template <class _Traits>
  896. basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete;
  897. #endif // _LIBCPP_STD_VER >= 20
  898. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>;
  899. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  900. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>;
  901. #endif
  902. #if _LIBCPP_STD_VER >= 23
  903. template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
  904. _LIBCPP_HIDE_FROM_ABI inline void
  905. __vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
  906. // [ostream.formatted.print]/3
  907. // Effects: Behaves as a formatted output function
  908. // ([ostream.formatted.reqmts]) of os, except that:
  909. // - failure to generate output is reported as specified below, and
  910. // - any exception thrown by the call to vformat is propagated without regard
  911. // to the value of os.exceptions() and without turning on ios_base::badbit
  912. // in the error state of os.
  913. // After constructing a sentry object, the function initializes an automatic
  914. // variable via
  915. // string out = vformat(os.getloc(), fmt, args);
  916. ostream::sentry __s(__os);
  917. if (__s) {
  918. string __o = std::vformat(__os.getloc(), __fmt, __args);
  919. if (__write_nl)
  920. __o += '\n';
  921. const char* __str = __o.data();
  922. size_t __len = __o.size();
  923. # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  924. try {
  925. # endif // _LIBCPP_HAS_NO_EXCEPTIONS
  926. typedef ostreambuf_iterator<char> _Ip;
  927. if (std::__pad_and_output(
  928. _Ip(__os),
  929. __str,
  930. (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
  931. __str + __len,
  932. __os,
  933. __os.fill())
  934. .failed())
  935. __os.setstate(ios_base::badbit | ios_base::failbit);
  936. # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  937. } catch (...) {
  938. __os.__set_badbit_and_consider_rethrow();
  939. }
  940. # endif // _LIBCPP_HAS_NO_EXCEPTIONS
  941. }
  942. }
  943. template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
  944. _LIBCPP_HIDE_FROM_ABI inline void vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args) {
  945. std::__vprint_nonunicode(__os, __fmt, __args, false);
  946. }
  947. // Returns the FILE* associated with the __os.
  948. // Returns a nullptr when no FILE* is associated with __os.
  949. // This function is in the dylib since the type of the buffer associated
  950. // with std::cout, std::cerr, and std::clog is only known in the dylib.
  951. //
  952. // This function implements part of the implementation-defined behavior
  953. // of [ostream.formatted.print]/3
  954. // If the function is vprint_unicode and os is a stream that refers to
  955. // a terminal capable of displaying Unicode which is determined in an
  956. // implementation-defined manner, writes out to the terminal using the
  957. // native Unicode API;
  958. // Whether the returned FILE* is "a terminal capable of displaying Unicode"
  959. // is determined in the same way as the print(FILE*, ...) overloads.
  960. _LIBCPP_EXPORTED_FROM_ABI FILE* __get_ostream_file(ostream& __os);
  961. # ifndef _LIBCPP_HAS_NO_UNICODE
  962. template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
  963. _LIBCPP_HIDE_FROM_ABI void __vprint_unicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
  964. # if _LIBCPP_AVAILABILITY_HAS_PRINT == 0
  965. return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);
  966. # else
  967. FILE* __file = std::__get_ostream_file(__os);
  968. if (!__file || !__print::__is_terminal(__file))
  969. return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);
  970. // [ostream.formatted.print]/3
  971. // If the function is vprint_unicode and os is a stream that refers to a
  972. // terminal capable of displaying Unicode which is determined in an
  973. // implementation-defined manner, writes out to the terminal using the
  974. // native Unicode API; if out contains invalid code units, the behavior is
  975. // undefined and implementations are encouraged to diagnose it. If the
  976. // native Unicode API is used, the function flushes os before writing out.
  977. //
  978. // This is the path for the native API, start with flushing.
  979. __os.flush();
  980. # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  981. try {
  982. # endif // _LIBCPP_HAS_NO_EXCEPTIONS
  983. ostream::sentry __s(__os);
  984. if (__s) {
  985. # ifndef _LIBCPP_WIN32API
  986. __print::__vprint_unicode_posix(__file, __fmt, __args, __write_nl, true);
  987. # elif !defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)
  988. __print::__vprint_unicode_windows(__file, __fmt, __args, __write_nl, true);
  989. # else
  990. # error "Windows builds with wchar_t disabled are not supported."
  991. # endif
  992. }
  993. # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  994. } catch (...) {
  995. __os.__set_badbit_and_consider_rethrow();
  996. }
  997. # endif // _LIBCPP_HAS_NO_EXCEPTIONS
  998. # endif // _LIBCPP_AVAILABILITY_HAS_PRINT
  999. }
  1000. template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
  1001. _LIBCPP_HIDE_FROM_ABI inline void vprint_unicode(ostream& __os, string_view __fmt, format_args __args) {
  1002. std::__vprint_unicode(__os, __fmt, __args, false);
  1003. }
  1004. # endif // _LIBCPP_HAS_NO_UNICODE
  1005. template <class... _Args>
  1006. _LIBCPP_HIDE_FROM_ABI void print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {
  1007. # ifndef _LIBCPP_HAS_NO_UNICODE
  1008. if constexpr (__print::__use_unicode_execution_charset)
  1009. std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), false);
  1010. else
  1011. std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);
  1012. # else // _LIBCPP_HAS_NO_UNICODE
  1013. std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);
  1014. # endif // _LIBCPP_HAS_NO_UNICODE
  1015. }
  1016. template <class... _Args>
  1017. _LIBCPP_HIDE_FROM_ABI void println(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {
  1018. # ifndef _LIBCPP_HAS_NO_UNICODE
  1019. // Note the wording in the Standard is inefficient. The output of
  1020. // std::format is a std::string which is then copied. This solution
  1021. // just appends a newline at the end of the output.
  1022. if constexpr (__print::__use_unicode_execution_charset)
  1023. std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), true);
  1024. else
  1025. std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);
  1026. # else // _LIBCPP_HAS_NO_UNICODE
  1027. std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);
  1028. # endif // _LIBCPP_HAS_NO_UNICODE
  1029. }
  1030. #endif // _LIBCPP_STD_VER >= 23
  1031. _LIBCPP_END_NAMESPACE_STD
  1032. _LIBCPP_POP_MACROS
  1033. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  1034. # include <atomic>
  1035. # include <concepts>
  1036. # include <cstdlib>
  1037. # include <iosfwd>
  1038. # include <iterator>
  1039. # include <stdexcept>
  1040. # include <type_traits>
  1041. #endif
  1042. #endif // _LIBCPP_OSTREAM