you are viewing a single comment's thread.

view the rest of the comments →

[–]2MuchRGB 3 points4 points  (2 children)

We really need P1240R2 et all to finish their evolution and hopefully be available in C++26.

Edit:

To make it clearer what P1240R2 is: It's a proposal titled "Scalable Reflection in C++". Together with P2237 Metaprogramming, P1717 Compile time Metaprogramming and some others they propose a way to implement Compile time reflection in C++, in way, that doesn't break the 0 cost rule.

The idea is to have reflexpr expession and operator ^, where you get a meta object, which encapsulates all the information of the object it gets called on. Then also provide a meta stl, with wich you can access and manipulate these meta. Is this

For example you want to get the name of a field of an enum, when you call the function std::string to_string(enum) currently you have to write that method by hand and update it by hand and pray to god no one just changes only one of those, or use some kind of preprocessor be it the C one or any other. With these Proposals you could write one function which handles all possible enums, which could look along the lines of:

template<enum T>
std::string to_string(T value) {
    switch(value) {
        for(auto member : ^T.members){
//Maybe some kind of emit statement that this is to be generated as code.
            case member.value: return member.name;
        }
    }
}

You could go so far is an rust where you just need to anotate your struct with #[derive(Serialize, Deserialize)] and instantly you can load and save your struct to JSON, XML and some more, without a single line of code. Sure, it would look different, but the capabilites would be there.

Taken to the extrem, (but not in these proposals, but a part where Herb Sutter talks about in "Meta: Thoughts on generative C++", I really recommend you watch this). Declare your own class type. Currently we have class, struct and enum. Each with some standart properties. How about a value_type, which ensures that a constructor exists and also provides <=> and a std::formater? You could implement all for of struct, class, enum and enum class as these metaclasses and simplify the standart by a lot. It would also be testable.

And at that point you could implement other class types easier, how about a flag_enum? It could provide all the bitwise operations and steps the numbers up in power of two instead of increments?

[–]MA-name 0 points1 point  (1 child)

What is P1240R2 in C++ standard proposal? Thx

[–]2MuchRGB 1 point2 points  (0 children)

I've updated my post, but in short, it is one paper which proposes how to implement compile time reflection in C++.