Document formatting of bit-fields and fields of packed structs

This commit is contained in:
Victor Zverovich 2024-12-21 11:06:11 -08:00
parent 7c3d0152e5
commit d8a79eafdc

View File

@ -511,7 +511,7 @@ chrono-format-specifications).
::: ptr(const std::shared_ptr<T>&) ::: ptr(const std::shared_ptr<T>&)
### Formatting Variants ### Variants
A `std::variant` is only formattable if every variant alternative is A `std::variant` is only formattable if every variant alternative is
formattable, and requires the `__cpp_lib_variant` [library formattable, and requires the `__cpp_lib_variant` [library
@ -527,6 +527,23 @@ feature](https://en.cppreference.com/w/cpp/feature_test).
fmt::print("{}", std::variant<std::monostate, char>()); fmt::print("{}", std::variant<std::monostate, char>());
// Output: variant(monostate) // Output: variant(monostate)
## Bit-Fields and Packed Structs
To format a bit-field or a field of a struct with `__attribute__((packed))`
applied to it, you need to convert it to the underlying or compatible type via
a cast or a unary `+` ([godbolt](https://www.godbolt.org/z/3qKKs6T5Y)):
```c++
struct smol {
int bit : 1;
};
auto s = smol();
fmt::print("{}", +s.bit);
```
This is a known limitation of "perfect" forwarding in C++.
<a id="compile-api"></a> <a id="compile-api"></a>
## Format String Compilation ## Format String Compilation