std::println(std::ostream)(3)
π std::println(std::ostream) β NAME
okay bestie, this is std::println(std::ostream), straight out of the C++ Standard Library (section 3 of the manual, iykyk). It's the "print it and then hit enter" queen of the <ostream> world. Let's get into it β¨
πΈ Synopsis
She lives here:
Defined in header <ostream>
The signatures, verbatim, no cap:
template< class... Args >
void println( std::ostream& os, std::format_string<Args...> fmt,
(1) (since C++23)
Args&&... args );
void println( std::ostream& os );
(2) (since C++26)
So what does she do? She formats args according to the format string fmt, with a '\n' appended β which means that each output ends with a new-line, bestie, every single time β and then inserts the result into the os stream. π
The overloads, spelled out:
- Equivalent to:
std::print(os, "{}\n", std::format(fmt, args...)); - Equivalent to:
std::print(os, "\n");
π«Ά Heads up: the behavior is undefined ifstd::formatter<Ti, char>does not meet theBasicFormatterrequirements for anyTiinArgs(as required bystd::make_format_args). So like... don't do that. Please. π
π¦ Parameters
osβ output stream to insert data intofmtβ an object that represents the format string. The format string consists of:- ordinary characters (except
{and}), which are copied unchanged to the output, - escape sequences
{{and}}, which are replaced with{and}respectively in the output, and - replacement fields.
- ordinary characters (except
Each replacement field has the following format:
{ arg-id (optional) } (1)
{ arg-id (optional) : format-spec } (2)
- replacement field without a format specification
- replacement field with a format specification
arg-idβ specifies the index of the argument inargswhose value is to be used for formatting; if it is omitted, the arguments are used in order. Thearg-ids in a format string must all be present or all be omitted. Mixing manual and automatic indexing is an error πformat-specβ the format specification defined by thestd::formatterspecialization for the corresponding argument. Cannot start with}.
Now, what that format-spec actually means depends on the type, and there's a whole little menu of vibes:
- For basic types and standard string types, the format specification is interpreted as standard format specification.
- For chrono types, the format specification is interpreted as chrono format specification.
- For range types, the format specification is interpreted as range format specification.
- For
std::pairandstd::tuple, the format specification is interpreted as tuple format specification. - For
std::thread::idandstd::stacktrace_entry, see thread id format specification and stacktrace entry format specification. (since C++23) - For
std::basic_stacktrace, no format specifier is allowed. π« - For
std::filesystem::path, see path format specification. (since C++26) - For other formattable types, the format specification is determined by user-defined formatter specializations.
args...β arguments to be formatted π
π Return value
(none)
Yeah. She gives you nothing back. She just does the thing and leaves. Main character energy honestly. π
β Exceptions
std::bad_allocon allocation failure.- Propagate any exception thrown by any formatter, e.g.
std::format_error, without regard to the value ofos.exceptions()and without turning onios_base::badbitin the error state ofos. - May throw
ios_base::failurecaused byos.setstate(ios_base::badbit)which is called if an insertion intoosfails.
π yeah the exception behavior is a whole thing β those formatter exceptions do NOT care about your stream's exception mask and will NOT flip badbit for you. Fr fr.
π Notes
Even though overload (2) is added in C++26, all known implementations make it available in C++23 mode. Slay, we love an early release. π«Ά
Feature-test macros, since tables aren't a thing here, list form:
__cpp_lib_printβ value202207L,(C++23), feature: Formatted output__cpp_lib_formatβ value202207L,(C++23), feature: Exposingstd::basic_format_string
π¦ Example
This section is incomplete.
Reason: no example.
(not me showing up to a manpage with zero example π it's giving "trust me bro". Anyway.)
π See also
print(std::ostream)(C++23) β outputs formatted representation of the arguments (function template)println(C++23) β same asstd::printexcept that each print is terminated by additional new line (function template)format(C++20) β stores formatted representation of the arguments in a new string (function template)
Also logged in the category: Todo no example. π
β¨ and that's the whole page, bestie β brought to you by http://cppreference.com, dated 2024.06.10, for std::println(std::ostream)(3). go forth and newline responsibly π
π¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms