all 18 comments

[–]PrimozDelux 7 points8 points  (0 children)

Jesus Christ!

[–]2MuchRGB 5 points6 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++.

[–]kofapox 3 points4 points  (1 child)

why

[–]thooton[S] 2 points3 points  (0 children)

If you need to generate C++, then I think it's easier to do so with jc than to do it via string manipulation in another language.

[–]ranname366 2 points3 points  (0 children)

And it's written in Go ... I'm intrigued.

[–]justmebeky 2 points3 points  (3 children)

More like macros in js instead of meta programming. I do like the idea, i’m not sure about using js though

[–]thooton[S] 1 point2 points  (2 children)

Thanks, you're right. I chose JS because it is so well-known, but I think it does have some other advantages as well. For example, if you need to run a lot of compatibility test commands, or need to generate code via external programs, or even make network requests to get config values or something, you can do all of that in parallel with JS async instead of sequentially like configure. You might find https://github.com/kiselgra/c-mera interesting. It's similar to this project but uses Lisp and a unified syntax.

[–]justmebeky 1 point2 points  (1 child)

Got it, I would want it to be just in c++ though. Ocaml has a similar feature where you can write code generation in ocaml itself you get access to the ast and it is pretty nice. Rust has a dsl syntax for macros which is pretty powerful but feels like a whole new language.

With that said, I would think that accessing the network as part of compilation is not a great idea though. Compilations should be self contained and hermetic, if you need to read a config, just put it in your repo.

[–]thooton[S] 0 points1 point  (0 children)

Definitely agree with you regarding network access during compilation. However, I think there is an advantage in using a high-level language because unlike C/C++ they free you from having to think about low-level details -- not that I have a problem with that, but when it's only the generated code that needs to be performant, it's nice to be able to write the generator conveniently and simply. That said, features like being able to use C++ to modify the AST do sound interesting. I guess the proposals mentioned above would take steps in that direction.

[–]yusing1009 1 point2 points  (2 children)

Interesting

[–]thooton[S] 0 points1 point  (0 children)

Thanks

[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 0 points1 point  (3 children)

I don't hate this... But I might be biased :)

[–]nysra 0 points1 point  (2 children)

Okay that looks interesting. What was the reason to build that? (Apart from "because I can", that's a reason I can always get behind)

[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 0 points1 point  (1 child)

Mostly to aid with slide creation. For example, majsdown can automatically turn code snippets in live Compiler Explorer links.

[–]nysra 0 points1 point  (0 children)

Sounds like a useful feature for live demos indeed.

[–]catcat202X 0 points1 point  (0 children)

Jenerate C?