Modern C++ › C++23
std::print and import std
Simpler output and standard library modules.
Explanation
std::print and std::println (header <print>) combine std::format with output: they format and write in one step, to stdout by default or to any FILE*/std::ostream.
std::println("{} + {} = {}", 2, 3, 5); // prints, then a newline
std::print(stderr, "error {}\n", code);
Why they beat std::cout <<:
- Shorter and clearer, with the full
{}format mini-language; - usually faster (no stream state, no repeated locale lookups, one write call);
- Unicode-correct: on Windows with a UTF-8 source/execution charset it writes valid UTF-8 to the console, which
printfandcoutoften garble; - type-safe and checked at compile time.
C++23 also lets std::format/print format ranges and tuples ({} on a std::vector<int> gives [1, 2, 3], and {:n} drops the brackets) and adds std::format_string.
import std; imports the entire standard library as a module in one line (and import std.compat; adds the global C names like ::printf). It’s much faster to compile than including headers and doesn’t leak macros. It needs a toolchain with the standard-library module built (MSVC 17.5+ with Build ISO C++23 Standard Library Modules; recent GCC/Clang+libc++ with CMake 3.30+).
Example
#include <map>
#include <print>
#include <string>
#include <vector>
int main() {
std::println("Hello, {}!", "C++23");
std::println("{:>8}|{:<8}|{:^8}|", 42, 42, 42);
std::println("{:.3f} {:#x} {:08b}", 3.14159, 255, 5);
std::vector<int> v{1, 2, 3};
std::println("vector: {}", v); // vector: [1, 2, 3]
std::println("no brackets: {:n}", v); // no brackets: 1, 2, 3
std::map<std::string, int> ages{{"Ann", 30}, {"Bob", 25}};
std::println("map: {}", ages); // map: {"Ann": 30, "Bob": 25}
auto pair = std::pair{1, "one"};
std::println("pair: {}", pair); // pair: (1, "one")
std::print("no newline, ");
std::print(stderr, "this goes to stderr\n");
std::println(); // just a newline
return 0;
}
The same program using the module import (in a project configured for it):
import std; // replaces every #include <...>
int main() {
std::println("Hello from import std!");
}
Common pitfalls
std::printwrites to the CFILE*stdout directly; mixing it withstd::coutwithoutstd::ios::sync_with_stdio(true)(the default) is fine, but avoid interleaving with unsynchronized streams.- The format string must be a compile-time constant; use
std::vformatfor runtime format strings. import std;is all-or-nothing and must come before any conflicting#includeof the same headers in the same file in some toolchains. Don’t mix freely.- Setting up
import std;is a build-system task (CMakeCXX_MODULE_STD, MSVC project option), not a code change. - Windows console: to see UTF-8 correctly, build with
/utf-8and use a UTF-8 console code page / Windows Terminal. - Support: MSVC 19.37+, GCC 14+, Clang 18+ (libc++). Check your library version.
Legacy vs modern
// C++98: printf is unsafe, iostream is verbose
printf("%s is %d\n", name.c_str(), age); // wrong specifier = undefined behavior
std::cout << name << " is " << age << "\n";
// C++23
std::println("{} is {}", name, age);
C++98 programs began with a wall of #includes: <iostream>, <string>, <vector>, <algorithm>… See Streams and Formatting and std::format and std::span, plus Modules for the machinery behind import std;.
Exercises
1. Print a std::vector<std::string> as comma-separated values without brackets.
{:n} removes the brackets but prints strings quoted ("a", "b"). For plain text, join them:
auto csv = names | std::views::join_with(std::string(", ")) | std::ranges::to<std::string>();
std::println("{}", csv); // a, b