Skip to content

Commit

Permalink
Document formatting of bit-fields and fields of packed structs
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Dec 21, 2024
1 parent 7c3d015 commit d8a79ea
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ chrono-format-specifications).
::: ptr(const std::shared_ptr<T>&)
### Formatting Variants
### Variants
A `std::variant` is only formattable if every variant alternative is
formattable, and requires the `__cpp_lib_variant` [library
Expand All @@ -527,6 +527,23 @@ feature](https://en.cppreference.com/w/cpp/feature_test).
fmt::print("{}", std::variant<std::monostate, char>());
// 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>
## Format String Compilation

Expand Down

0 comments on commit d8a79ea

Please sign in to comment.