CppCon 2016: "C++14 Reflections Without Macros, Markup nor External Tooling.."
Why Stack Overflow recommends it
Stack Overflow users link it when someone asks how to count, iterate over or print the fields of a plain C++ struct without native reflection.
Recommended by 4 people across 6 different months, 2016 to 2021.
What it is
A CppCon 2016 talk by Antony Polukhin (author of Boost.TypeIndex and Boost.DLL) showing how C++14 can reflect on plain structs without macros or external tools. It builds up from counting fields by probing braced initialization to deducing each field's type, using variadic templates, constexpr, SFINAE and implicit conversion operators. The work became the magic_get / Boost.PFR library.
What people said
Recommends einpoklum · 2016-10 · Stack Overflow · on “C++ std::tuple order of destruction”
This issue has very recently come up in Antony Polukhin's excellet CPPCon 2016 talk [this video] on achieving partial reflection in C++14 without proper language support for it. IIRC he says the only scary thing he had to do reimplement std::tuple to enforce a certain order of initialization. Also, ping @101010 on this.
Recommends einpoklum · 2017-08 · Stack Overflow · on “get the number of fields in a class”
Indeed, Antony Polukhin has shown us that C++ does have (some) reflection, since C++14, without knowing it; and that you can extract information about the fields. ... well, at least for plain-old-data structs/classes. Watch his CppCon 2016 talk: C++14 Reflections Without Macros, Markup nor External Tooling / Antony Polukhin [this video] And then you use: template <class T> constexpr std::size_t fields_count() noexcept; which gets you the field count. To use that you need these two file: [LINK] [LINK] and that should be enough.
Recommends Julius · 2017-10 · Stack Overflow · on “How to enumerate all member variables of a class / struct in c++”
Not an answer to your problem but probably highly related: There exists an early-stage library for reflection without macros [LINK] with techniques explained in a conference talk recording [this video] .
Recommends Julius · 2017-12 · Stack Overflow · on “using C++ template programming to extract the field types of an arbitrary structure”
The behavior you describe is called reflection [LINK] . The C++ language features which support this feature are very limited (yet). However, with some tricks it is possible to achieve some level of reflection. One example is Boost.PFR (Precise and Flat Reflection) by Antony Polukhin. PFR is an official boost library since December 2020 (boost 1.75.0), but according to its documentation [LINK] it is a header only library that does not depend on Boost. Here is one example from the README [LINK] : #include <iostream> #include <string> #include "boost/pfr.hpp" struct some_person { std::string name; unsigned birth_year; }; int main() { some_person val{"Edgar Allan Poe", 1809}; std::cout << boost
Recommends Sarfaraz Nawaz · 2018-02 · Stack Overflow · on “Split a struct into a tuple”
@GuillaumeRacicot: Thanks for the link to **magic_get**. I was looking for that. Here is the talk at CppCon [this video] that explains it in great details.
Recommends 康桓瑋 · 2021-02 · Stack Overflow · on “Is there a way to specify and use a list of all data members belonging to a C++ class”
This is no way to do this without reflection support. The alternative way is to transform your customized struct into the tuple of your member reference then using std::apply to operate the elements of the tuple one by one. You can see CppCon 2016: "C++14 Reflections Without Macros, Markup nor External Tooling" [this video] for the details. Here are the concepts: First, we need to detect your customized struct 's fields count: template <auto I> struct any_type { template <class T> constexpr operator T& () const noexcept; template <class T> constexpr operator T&&() const noexcept; }; template <class T, auto... Is> constexpr auto detect_fields_count(std::index_sequence<Is...>) noexcept { if constex
Cites amin · 2016-10 · Stack Overflow · on “How can I make C++ ADL to look into all instances of a template?”
I'm following the How to implement a constant-expression counter in C++ [LINK] tutorial and I'm trying to fix the C++14 Reflections Without Macros, Markup nor External Tooling [this video] talk limitations. The basic idea of the tutorial is this: template<int N> struct flag { friend constexpr int adl_flag (flag<N>); }; template<int N> struct writer { friend constexpr int adl_flag (flag<N>) { return N; } static constexpr int value = N; }; template<int N, class = char[noexcept(adl_flag(flag<N> ()))?+1:-1]> int constexpr reader (int, flag<N>) { return N; } template<int N> int constexpr reader (float, flag<N>, int R = reader (0, flag<N-1>())) { return R; } int constexpr reader (float, flag<0>) { retu
Cites krzaq · 2016-11 · Stack Overflow · on “How to impose a lexicographic order on an (arbitrary) POD C++ struct?”
You can do this in C++1z. Basing on this [LINK] answer, I prepared the following proof of concept: struct anything { template<class T> operator T()const; }; namespace details { template<class T, class Is, class=void> struct can_construct_with_N:std::false_type {}; template<class T, std::size_t...Is> struct can_construct_with_N<T, std::index_sequence<Is...>, std::void_t< decltype(T{(void(Is),anything{})...}) >>: std::true_type {}; } template<class T, std::size_t N> using can_construct_with_N=details::can_construct_with_N<T, std::make_index_sequence<N>>; namespace details { template<std::size_t Min, std::size_t Range, template<std::size_t N>class target> struct maximize: std::conditional_t< ma
Cites lorro · 2017-01 · Stack Overflow · on “Comparing objects in C and C++”
The pragmatic C++-way of printing an object is defining a friend function: std::ostream& operator<<(std::ostream& os, const Point& point) { return os << "(" << point.x << "," << point.y << ")"; } It's usually class-specific so you need to implement it yourself; however, you might use some form of reflection. Particularly interesting is a CppCon-talk from Antony Polukhin [1] which gives reflection for POD types (like Point above). Generic reflection without external tools is N/A yet (as of 2016), there's a proposal on it. If you can't / don't want to wait, you can do multiple things: Parse C++ code: ctags comes to mind. Macros: It's relatively easy to write a FIELDS macro that defines a refle
Cites Nir · 2017-06 · Stack Overflow · on “Iterating over a struct in C++”
In C++14 it become possible, see this lecture: CppCon 2016: C++14 Reflections Without Macros, Markup nor External Tooling.. [this video]