magic_get VS pfr

Compare magic_get vs pfr and see what are their differences.

magic_get

std::tuple like methods for user defined types without any macro or boilerplate code (by apolukhin)

pfr

std::tuple like methods for user defined types without any macro or boilerplate code (by boostorg)
Our great sponsors
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • SaaSHub - Software Alternatives and Reviews
magic_get pfr
9 4
193 1,261
- 1.3%
8.2 8.1
5 months ago 9 days ago
C++ C++
Boost Software License 1.0 Boost Software License 1.0
The number of mentions indicates the total number of mentions that we've tracked plus the number of user suggested alternatives.
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.

magic_get

Posts with mentions or reviews of magic_get. We have used some of these posts to build our list of alternatives and similar projects. The last one was on 2023-02-11.
  • What is a good way to iterate through struct contents?
    2 projects | /r/cpp_questions | 11 Feb 2023
    Maybe this: https://github.com/apolukhin/magic_get
  • What information about a type/class can we get?
    1 project | /r/cpp | 17 Dec 2022
    You can access non-static member variables with structured bindings. See for instance magic_get/boostPfr https://github.com/apolukhin/magic_get. Structured bindings will only bind to the accessible members (not private). Magic get works by by finding the number of member variables, converting the struct to a tuple (with a function that is specialized for the number of fields), and finally accessing the members through the tuple (supporting indexing and iteration). It does not grant access to the member names, but it is sufficient for some reflection.
  • A way to determine the number of elements in a structured binding
    1 project | /r/cpp | 10 Jun 2022
    Libraries like magic_get expose the members of an aggregate class/struct to allow writing generic code for things like pretty printing and serialization without anything special done to the class itself. They often rely on structured bindings for the decomposition (*), but find the number of elements via SFINAE on aggregate initialization, as an aggregate type can be initialized only from as many objects as it has members. It would be nicer if you could SFINAE directly on the structured binding itself, as then the type could have user-defined constructors (which aggregates can't). Unfortunately, this is not possible since structured binding is a statement and not an expression. Unless you're using Clang, where the GNU statement expressions extension allows you to do SFINAE on them, as in here.
  • Minimum viable declarative GUI in C++
    3 projects | /r/cpp | 23 Mar 2022
    No RTTI required, but the types are required to be aggregates (no constructors defined). It's possible to count the number of members using SFINAE by trying different numbers of inputs to the aggregate constructor using a type that's castable to anything, and then enumerate the members with a similar trick (or use structured binding to pull them out directly). I think he uses magic_get which is the most popular library for this trick.
  • Call function on each member of struct using preprocessor
    1 project | /r/cpp_questions | 21 Mar 2022
    take a look at magic_get to get access to all struct members. No idea what your plan is with foo and the preprocessor though.
  • Serdepp 0.1.2 Released
    2 projects | /r/cpp | 18 Sep 2021
    Neat! Have you considered using magic_get?
  • Getting information about classes, methods and variables in C++?
    4 projects | /r/cpp_questions | 13 Aug 2021
    It is possible with some hacks https://github.com/apolukhin/magic_get
  • Struct bulk operations - Reflection? Code gen?
    1 project | /r/cpp_questions | 19 Apr 2021
  • Reflecting Over Members of an Aggregate
    1 project | /r/cpp | 21 Mar 2021
    I actually reference it near the bottom of the article under its original name, magic_get! I was disappointed to discover that this library did it a similar way first before me while researching 😅

pfr

Posts with mentions or reviews of pfr. We have used some of these posts to build our list of alternatives and similar projects. The last one was on 2022-03-23.
  • Rooting for P1061 "Structured Bindings can introduce a Pack"
    1 project | /r/cpp | 19 Oct 2023
    This single feature opens a world of new possiblities. For example, it makes implementing "getting the number of fields" trivial. Furthrmore, and much more importantly, it enables turning a struct into a tuple. Currently, this can only be done by enumerating cases (therefore it's not fully generic), as with Boost PFR. By the way, PFR greatly simplifies our codebases, especially for parts with serialization and/or reflection.
  • Minimum viable declarative GUI in C++
    3 projects | /r/cpp | 23 Mar 2022
    The code is relatively short and can be groked with a few coffees: https://github.com/boostorg/pfr/tree/develop/include/boost/pfr ; if you're using C++17 it uses a binary search (https://github.com/boostorg/pfr/blob/develop/include/boost/pfr/detail/fields_count.hpp) to count the number of fields in a struct, by starting by the observation that a likely majorant on the number of fields in a struct is sizeof(the struct) * CHAR_BIT, assuming not too many [[no_unique_address]] tomfooleries. Then once this count is known it's possible to simply map them as a tuple through sheer brute force and destructuring: https://github.com/boostorg/pfr/blob/develop/include/boost/pfr/detail/core17_generated.hpp
  • The Serde Rust Framework
    14 projects | news.ycombinator.com | 14 Oct 2021
    I wonder if the c++ approach of boost.pfr would be portable to rust ? It allows reflection on aggregates without needing to annotate anything: https://github.com/boostorg/pfr
  • Counting the number of fields in an aggregate in C++20
    2 projects | /r/cpp | 14 Mar 2021
    It is an 'interesting' meta-programming problem though (wasted many weeks on it myself, fixed a small gcc bug - a 'uniform init' edge case and filed an issue with magic_get Reflecting array members of aggregate structs).

What are some alternatives?

When comparing magic_get and pfr you can also consider the following projects:

cppreference-doc - C++ standard library reference

Magic Enum C++ - Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code

cling - The cling C++ interpreter

MLV-App - All in one MLV processing app.

serdepp - c++ serialize and deserialize adaptor library like rust serde.rs

ComLightInterop - Cross-platform COM interop library for .NET Core 2.1 or newer

create-rust-app - Set up a modern rust+react web app by running one command.

EU4ConsolePatcher - A simple memory patcher which enables the internal developer console in ironman mode

sapio - A Bitcoin Programming Language

manifold - Manifold is a Java compiler plugin, its features include Metaprogramming, Properties, Extension Methods, Operator Overloading, Templates, a Preprocessor, and more.

miniserde - Data structure serialization library with several opposite design goals from Serde

clang-tutor - A collection of out-of-tree Clang plugins for teaching and learning