3 ms·
We just need an interpreter that can work on C++ files, supported by the standard. Something akin to the https://www.python.org/dev/peps/pep-0638/ https://www.
by elitepleb 6y ago
We just need an interpreter that can work on C++ files, supported by the standard.
Something akin to the https://www.python.org/dev/peps/pep-0638/ https://www.python.org/dev/peps/pep-0638/
Code generators/transformers are rare only because it's so hard to actually start.
Too bad https://www.circle-lang.org https://www.circle-lang.org never took off.
- wwright 6y agoIsn’t that what templates and constexpr are?
- tylerhou 6y agoTo a degree, but templates and constexpr don't support a bunch of features like compile-time field enumeration and introspection + code generation. For example, let's say I have a bunch of structs: struct GeoCoordinate { int lat, long; }; struct GeoArea { std::vector<GeoCoordinate> perimeter; }; struct Place { std::string name; std::string contact_number; GeoArea area; }; Now I need to serialize these structs into a format to be sent over the wire. Currently, I have a few choices: 1. Use an off-the-shelf library like protobuf (disclaimer: I work for Google). Then I have to convert my code to a protobuf definition and rely on its code generator to perform [de]serialization. I also have to hope that my library supports all the field definitions I need. 2. Write macros to define each field in each structure. These macros perform some arcane magicks that somehow create the necessary [de]serialization functions. These macros are difficult to write and maintain (or I find a library). 3. Manually define the methods myself. This is tedious, hard to maintain, and error prone. What if I could write some code in C++ which could read the structure and generate the appropriate serialization code? Something like (syntax hypothetical): Serializable(Class) { std::string serialize() { std::string output; for (auto member : Class.members()) { // loop unrolled at compile time if (member.type == int) { output.append(std::format("{:10}"), member.get()) } else if (member.type == std::string) { ... } else if (member.type == std::vector) { ... } else if (std::has_metaclass_v<member.type, Serializable>) { output.append(member.get().serialize()); } } }; }; Then I could annotate my classes with Serializable instead. See https://www.youtube.com/watch?v=4AfRAVcThyA https://www.youtube.com/watch?v=4AfRAVcThyA.
- kthielen 6y agoYeah static structural reflection is an important use-case. The main objection I’ve heard to standardization in C++ has to do with maintaining the mistake of conflating structs and classes and supporting C’s long broken unit type.
- tylerhou 6y agoA few years ago Herb Sutter proposed Python-like metaclasses in C++ through compile-time code generation. Not sure if anything has been proposed for general use. https://www.youtube.com/watch?v=4AfRAVcThyA https://www.youtube.com/watch?v=4AfRAVcThyA
- OneGuy123 6y agoUnreal Engine 4 creates C++ reflection classes by parsing the headers and some of their custom macros that you define for member variables & function. It basicaly allows you full C++ reflection, but only for classes that you mark with the UE4 macros.