正常
std::print (C++23)
TL;DR
Output formatted strings directly to stdout—a combination of printf + iostream + type safety, the new way to write Hello World in C++23.
Header
cpp
#include <print>Core API Cheat Sheet
| Operation | Signature | Description |
|---|---|---|
| Output to stdout | std::print(fmt, args...); | Format and output to standard output |
| Output with newline | std::println(fmt, args...); | Automatically append a newline character |
| Empty line | std::println(); | Output only a newline character |
| Output to file | std::print(file, fmt, args...); | Output to a specified C file stream |
| Output to file with newline | std::println(file, fmt, args...); | Newline version |
| Output to stream | std::print(stream, fmt, args...); | Output to a C++ stream |
Minimal Example
cpp
#include <print>
int main() {
// Basic replacement
std::print("Hello, {}!\n", "World");
// Automatic newline
std::println("The answer is {}", 42);
// User-defined types (if formatter is specialized)
// std::println("Point: {}", Point{10, 20});
}Embedded Applicability: Low
- Relies on the OS and filesystem abstraction layer; bare-metal environments typically lack standard output.
- Suitable for logging in embedded Linux host tools or test frameworks.
- The formatting engine incurs significant Flash overhead; it is not recommended for resource-constrained devices.
- Use
fmtlibrary'sfmt::printas a fallback option starting from C++11.
Compiler Support
| GCC | Clang | MSVC |
|---|---|---|
| 14 | 18 | 19.34 |
See Also
Part of the content references cppreference.com, licensed under CC-BY-SA 4.0