std::println(3)
π std::println(3) β C++ Standard Libary, but make it cute π
okay so bestie, welcome to the std::println(3) page, straight from the C++ Standard Libary. this is the overload family that formats your args according to a format string, appends '\n' so every output ends with a new-line, and prints the result to a stream. iconic. π
πΈ NAME πΈ
std::println β std::println (yes, she named the section after herself π
)
β¨ Synopsis β¨
Defined in header <print>.
template< class... Args > (1)
(since C++23)
void println( std::format_string<Args...> fmt, Args&&... args );
template< class... Args >
void println( std::FILE* stream, (2)
(since C++23)
std::format_string<Args...> fmt, Args&&... args );
void println(); (3)
(since C++26)
void println( std::FILE* stream ); (4)
(since C++26)
so what does she do? She formats args according to the format string fmt with an appended '\n' β which means that each output ends with a new-line β and prints the result to a stream. π
1)Equivalent tostd::println(stdout, fmt, std::forward<Args>(args)...).2)Equivalent tostd::print(stream, std::runtime_format(std::string(fmt.get()) + '\n'), std::forward<Args>(args)...).3)Equivalent tostd::println(stdout).4)Equivalent tostd::print(stream, "\n").
π¨ warning bestie: the behavior is undefined ifstd::formatter<Ti, char>does not meet theBasicFormatterrequirements for anyTiinArgs(as required bystd::make_format_args). no cap, take that seriously.
π¦ Parameters π¦
streamβ output file stream to write to. π«Άfmtβ 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}.
then the flavor of format-spec depends on the type, bestie:
- 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 (since C++23) thread id format specification and stacktrace entry format specification. - For
std::basic_stacktrace, no format specifier is allowed. - For
std::filesystem::path, see path format (since C++26) specification. - For other formattable types, the format specification is determined by user-defined
formatterspecializations. args...β arguments to be formatted. π
π Exceptions π
std::bad_allocon allocation failure.std::system_error, if writing to the stream fails.- Propagates any exception thrown by used formatters, e.g.
std::format_error.
π Notes π
Although overloads (3,4) are added in C++26, all known implementations make them available in C++23 mode. slay for backwards-ish vibes. β¨
π§ Feature-test macro tea π§
__cpp_lib_printβ value202207L, std(C++23), feature: Formatted output.__cpp_lib_printβ value202403L, std(C++26), feature: Formatted output with stream locking(DR23).__cpp_lib_formatβ value202207L, std(C++23), feature: Exposingstd::basic_format_string.
π Example π
run this code, bestie:
// Run this code
#include <print>
int main()
{
// Each call to std::println ends with new-line
std::println("Please"); // overload (1)
std::println("enter"); // (1)
std::print("pass");
std::print("word");
std::println(); // (3); valid since C++26; same effect as std::print("\n");
}
π Output π
Please
enter
password
π§ See also π§
print(C++23) β prints to stdout or a file stream using formatted representation of the arguments (function template).println(std::ostream)(C++23) β outputs formatted representation of the arguments with appended'\n'(function template).format(C++20) β stores formatted representation of the arguments in a new string (function template).printf,fprintf,sprintf,snprintf(C++11) β prints formatted output to stdout, a file stream or a buffer (function).
big sparkle kiss from http://cppreference.com π β page dated 2024.06.10, and yes this was std::println(3). byeeee π«Άβ¨
π¦ 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