https://github.com/martinitus/fmt-reflect
Simple reflection experiments with libfmt and C++20
https://github.com/martinitus/fmt-reflect
libfmt reflection
Last synced: 10 months ago
JSON representation
Simple reflection experiments with libfmt and C++20
- Host: GitHub
- URL: https://github.com/martinitus/fmt-reflect
- Owner: martinitus
- Created: 2020-06-30T15:43:00.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-01T15:22:01.000Z (almost 6 years ago)
- Last Synced: 2025-03-06T17:16:07.714Z (over 1 year ago)
- Topics: libfmt, reflection
- Language: C++
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Formatting user objects with `libfmt`
This repo shows an example how to use [libfmt](https://github.com/fmtlib/fmt) and _manually configured static reflection_ to format user defined
structures.
Essentially it provides template specializations for `fmt::formatter` that allow formatting of structs for which
reflection information was setup via the `reflection` template.
Thanks to [this blog](https://wgml.pl/blog/formatting-user-defined-types-fmt.html) which helped a lot :-)
Example:
```c++
struct Inner {...};
struct Outer {...};
template<>
struct reflection {
/*definition of class name and field names has to provided manually...*/
};
template<>
struct reflection {
/*definition of class name and field names has to provided manually...*/
};
auto outer = Outer{.a=1,.b="hello",.inner={.x=3.12,.y=" ",.z=2}};
std::string simple = fmt::format("{:s}", outer); // :s means format as simple
assert(simple == "a|hello|3.12| |2");
std::string extended = fmt::format("{:e}",outer); // :e means format as extended
assert(extended == "Outer{.a=1, .b=hello, .inner=Inner{.x=3.12, .y= , .z=2}}");
```