The C++ Type Loophole (C++14) - Alexandr Poltavsky, software developer

submited by
Style Pass
2021-07-28 23:30:04

Recently I was doing some work on retrieving struct data member types. And as I was exploring, researching and testing ideas I uncovered this pearl:

Interesting. I had so much pain creating struct reader to detect data member types of a struct. And it was painfully limited: only literal types, pre-built type list, etc. With this anything becomes possible.

After some compiler-fight I came up with an elegant ten lines of code header that can create a type list from a data structure (with certain limitations) with just C++, no fixed type lists. Here is a short example of what it can do:

More examples: we can turn a structure into luple: a lightweight tuple of my design that has the advantage of a stable layout across compilers (at least as far as I tested it):

If you want to know more about how it works and its limitations then check out the code. I provide full commented source code of both the type loophole and luple in the GitHub repo. It works on all three major compilers.

Leave a Comment