CppCon 2016 has ended
Back To Schedule
Wednesday, September 21 • 4:45pm - 5:45pm
C++14 Reflections Without Macros, Markup nor External Tooling: metaprogramming tricks for POD types

Log in to save this to your schedule, view media, leave feedback and see who's attending!

C++ was lacking the reflections feature for a long time. But a new metaprogramming trick was discovered recently: we can get some information about POD structure by probing it's braced initializes. Combining that trick with variadic templates, constexpr functions, implicit conversion operators, SFINAE, decltype and integral constants we can count structure's fields and even deduce type of each field.

Now the best part: everything works without any additional markup nor macros typically needed to implement reflections in C++.

In this talk I'll explain most of the tricks in detail, starting from a very basic implementation that is only capable of detecting fields count and ending up with a fully functional prototype capable of dealing with nested PODs, const/volatile qualified pointers, pointers-to-pointers and enum members. Highly useful use-cases will be shown a the end of the talk. You may start experimenting right now using the implementation at https://github.com/apolukhin/magic_get.

Small motivating example:

#include <iostream>
#include "magic_get.hpp"

int main() {
    struct foo { // no `ostream& operator <<` defined for `foo`!
        double d;
        char c;

   foo f{ 1.0, '!' };
   std::cout << f; // Outputs `{1.0, !}`

avatar for Antony Polukhin

Antony Polukhin

Hi, I'm Antony Polukhin, the author of Boost.TypeIndex and Boost.DLL libraries; maintainer of the Boost.LexicalCast, Boost.Any, Boost.Variant and Boost.Conversion libraries. I'm also the author of of the "Boost C++ Application Development Cookbook"

Wednesday September 21, 2016 4:45pm - 5:45pm PDT
Bowie Hall (1st Floor Hall) Meydenbauer Center