Better C++14 reflections - Antony Polukhin - Meeting C++ 2018

Meeting Cpp · 6K views · 2019 · 45:16

Open on YouTube

C, C++, Rust, Go & systems languagesCompilers, interpreters & program analysistalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering how to iterate over struct members or deduce field types and constructor arguments without manual registration, since it explains how Boost.PFR works.

Recommended by 5 people across 7 different months, 2019 to 2025.

What it is

Antony Polukhin's Meeting C++ 2018 talk explains how to do struct reflection in C++14 without macros, using aggregate initialization tricks to count fields and deduce their types. It covers the internals of his magic_get library, later Boost.PFR, including its limits.

What people said

Recommends Arne Vogel · 2019-01 · Stack Overflow · on “Count the number of arguments in a lambda”

Definitely check out this talk [this video] by Antony Polukhin. It shows how you can not only deduce the number of arguments but also their types.

Recommends einpoklum · 2019-03 · Stack Overflow · on “Is there a way to add references to C++ class members to a vector without explicitly adding them?”

Yes, there is a a way to do directly what you're asking for, in C++14 at least, but it involves a bunch of dark voodoo. In this Meeting C++ 2018 talk: Better C++14 reflections - Antony Polukhin [this video] Antony explains how is able to iterate over all members of a struct with access to their types. He uses it to print values on some output stream, but you could use the same method to push back addresses into a std::vector<MyDescriptor*> . Having said that - I think you should just avoid this pattern. Just create a vector of MyDescriptors to begin with, and work with a reference to that. Why use a vector of pointers that requires so much contortion to produce?

Recommends Maxim Egorushkin · 2019-06 · Stack Overflow · on “How can I detect whether a class has an implicit constructor AND primitive members in c++?”

@Timo See the author's talk with full details: Better C++14 reflections - Antony Polukhin - Meeting C++ 2018 [this video] .

Recommends einpoklum · 2020-11 · Stack Overflow · on “Is there a way to store multiple types of struct members pointers”

(I'll first say I agree with @MarekR's comment, that your question is likely an "XY problem" [LINK] and you probably don't actually want to do this at all... but still:) This is an interesting challenge - and one which has been tackled by the "crazy genius" Antony Polukhin, in his magic_get [LINK] library - providing you are using the C++14 language standard, or later. There is actually no need to store anything! The struct definition itself has all the information you need. Thus, when you write: #include <iostream> #include <string> #include "boost/pfr.hpp" // <- Not formally a part of Boost, yet... // you'll need to download the library from github struct Foo { int a = 42; int b = 16; std:

Recommends Marek R · 2021-10 · Stack Overflow · on “Find the list of member variables of a class and their types?”

I always have to remember that if I add another variable int k to the class, I'll also have to add it in the initialization list k(soc.k) and sometimes in the destructor too. No you do not have to. Just leave everything public and do not provide constructors destructors. Aggregate initialization [LINK] will handle that. If you need something private this means you have encapsulation and list of constructors should be fixed no matter what are the fields. If you are warring about resources leak just use RAII pattern, in case of pointers std::unique_ptr and std::shared_ptr Now about title: Find the list of member variables of a class and their types? If you can use Aggregate initialization [LIN

Recommends v.oddou · 2022-09 · Stack Overflow · on “Printing the values of an instantiated structure without using the names of the fields in C++”

May I suggest: boost/pfr It will work with any aggregate type in C++17 (not limited to only strings) And it has the good taste of not depend on the whole boost, so you can just copy the pfr subfolder. more intel: [LINK] code: #include <boost/pfr.hpp> #include <string> #include <iostream> using std::string; struct bag { string fruit; string book; string money; }; int main() { bag b {"apple", "Computer Networking", "100"}; std::cout << boost::pfr::io(b); } output: Program returned: 0 {"apple", "Computer Networking", "100"} running example: [LINK] conference from Antony Polukhin on the inner workings of pfr: [this video]

Recommends Marek R · 2025-05 · Stack Overflow · on “reflect-cpp doesn't work with anything but basic structures”

This looks like some re-implementation of `boost::pfr` library. Here author explains [this video] how it works and what are the limitations. I'm pretty usre this library has same limitation. Basically to make it work aggregate initialization is required and you will disable that when defining any constructor.

Cites Dení · 2019-02 · Stack Overflow · on “C++ obtaining the type of a constructor”

There is a solution that allows you to obtain constructor parameters types. Note: it finds the first ctor having unambiguous and shortest set of parameters. Look at my example here: [LINK] In your example, the statement refl::as_tuple<foo> will result in std::tuple<int, double> . Once you have this tuple type, you can whatever you want, including foo type instantiation. The code above is based on a solution for determining types used for aggregate-init extended to handle user-defined ctors. Related materials: [LINK] [LINK] by Alexandr Poltavsky, [LINK] [this video] Better C++14 reflections - Antony Polukhin - Meeting C++ 2018