# Text formatting in C++ using libc++

DevFeed: [Text formatting in C++ using libc++](<https://devfeed.tech/articles/text-formatting-in-c-using-libc-43087.md>)

Original publisher: [Read original article](<https://blog.llvm.org/posts/2022-08-14-libc++-format/>)

Author: Mark de Wever

Published: 2022-09-27T00:00:00Z

Content type: tutorial

Language: en

Sources: [LLVM Project](<https://devfeed.tech/sources/the-llvm-project-blog.md>)

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [formatting](<https://devfeed.tech/topics/formatting.md>), [LLVM](<https://devfeed.tech/topics/llvm.md>), [implementation](<https://devfeed.tech/topics/implementation.md>)

Tags: [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [compilers](<https://devfeed.tech/tags/compilers.md>), [formatting](<https://devfeed.tech/tags/formatting.md>), [libc-plus-plus](<https://devfeed.tech/tags/libc-plus-plus.md>), [llvm](<https://devfeed.tech/tags/llvm.md>), [standard-library](<https://devfeed.tech/tags/standard-library.md>), [std-format](<https://devfeed.tech/tags/std-format.md>), [unicode](<https://devfeed.tech/tags/unicode.md>)

## AI overview

This tutorial introduces C++20 std::format, explains how it improves text formatting compared with stream operators and printf, and describes implementation status in LLVM 15. It also covers formatting options, user-defined types, Unicode grapheme clusters, column widths, and compile-time type errors.

## Source excerpt

Historically formatting text in C++, using the standard library, has beenunpleasant. It is possible to get nice output with the stream operators, but itis very verbose due to the stream manipulators. The other option is usingprintf, which is convenient but only supports a limited number of types andis not extendable. A non-standard option is using the{fmt} library. This article provides ashort introduction to the parts of this library that were standardized in C++20as std::format, as well as the current implementation status in LLVM 15. What is std::format std::format is a text formatting library using format strings similar toPython's formatand extensible for user defined types. #include <format>#include <iostream>int main() { std::cout << std::format("Hello {} in C++{}", "std::format", 20);} Writes the following output: Hello std::format in C++20 The {} indicates a replacement field like % in printf. Withstd::format the argument types are known, so it is not required to specifythem in the replacement field. The desired output format and the positional argument to use for eachreplacement field can also be specified.(For brevity, the following examples omit the required includes.) Writes the first positional argument using different bases, a prefix, andzero padding to 8 columns. int main() { std::cout << std::format("{0:#08b}, {0:#08o}, {0:08}, {0:#08x}", 16);} 0b010000, 00000020, 00000016, 0x000010 It is possible to use an upper case prefix and hexadecimal digits. int main() { std::cout << std::format("{0:#08B}, {0:#08o}, {0:08}, {0:#08X}", 15);} 0B001111, 00000017, 00000015, 0X00000F The alignment and fill character can be specified. int main() { std::cout << std::format("{:#<8} {:*>8} {:-^5}", "Hello", "world", '!');} Hello### ***world --!-- When printing tables it is nice to be able to specify the alignment and widthof the columns. However, formatting Unicode text can be especially tricky,since not every char (or wchar_t) is one "character". For example the lett