Provides a printf -like interface to format s with args and puts it into os .
Use {} as a placeholder within your format string s .
- By default,
os << t will be used to stream the appropriate argument: print(os, "int: {}, float: {}", 23, 42.f);
std::ostream & print(std::ostream &os, const char *s) Base case.
- You can also place a function as argument to inject arbitrary code:
print(os, "int: {}, fun: {}, float: {}", 23, [&]() { os << "hey :)"}, 42.f);
- There is also a variant to pass
os to the function, if you don't have easy access to it: print(os, "int: {}, fun: {}, float: {}", 23, [&]( auto& os) { os << "hey :)"}, 42.f);
- You can put a
std::ranges::range as argument. This will output a list - using the given specifier as separator. Each element elem of the range will be output via os << elem : std::vector<int> v = {0, 1, 2, 3};
- If you have a
std::ranges::range that needs to be formatted in a more complicated way, bundle it with an Elem: std::vector<int> v = {3, 2, 1, 0};
size_t i = 0;
print(os, "v: {, }", Elem(v, [&]( auto elem) { print(os, "{}: {}", i++, elem); }));
- You again have the option to pass
os to the bundled function: std::vector<int> v = {3, 2, 1, 0};
size_t i = 0;
print(os, "v: {, }", Elem(v, [&]( auto& os, auto elem) { print(os, "{}: {}", i++, elem); }));
* Use `{{` or `}}` to literally output `{` or `{`:
- See also
- Tab
|