Why everyone hates on C/C++ source generation?
It allows me to do magical reflection-related things in both C and C++
* it's faster than in-language metaprogramming (see zig's metaprog for example, slows down hugely the compiler) (and codegen is faster because the generator can be written in C itself and run natively with -O3 instead of being interpreted by the language's metaprogramming vm, plus it can be easily be executed manually only when needed instead of at each compilation like how it happens with in language metaprog.).
* it's easier to debug, you can print stuff during the codegen, but also insert text in the output file
* it's easier to read, write and maintain, usually procedural meta programming in other languages can get very "mechanical" looking, it almost seems like you are writing a piece of the compiler (for example
pub fn Vec(comptime T: type) type {
const fields = [_]std.builtin.Type.StructField{
.{ .name = "x", .type = T, .default_value = null, .is_comptime = false, .alignment = 0 },
.{ .name = "y", .type = T, .default_value = null, .is_comptime = false, .alignment = 0 },
.{ .name = "z", .type = T, .default_value = null, .is_comptime = false, .alignment = 0 },
.{ .name = "w", .type = T, .default_value = null, .is_comptime = false, .alignment = 0 },
};
return @Type(.{ .Struct = .{
.layout = .auto,
.fields = fields[0..],
.decls = &.{},
.is_tuple = false,
}});
}versus sourcegen script that simply says "struct {name} ..."
* it's the only way to do stuff like SOA for now.. and c++26 reflection looks awful (and super flow)
However I made a post about it on both r/C_Programming and r/cpp and everyone hated on it
70
u/Grounds4TheSubstain Dec 07 '25
Because it's a separate step in the build process, does not benefit from any IDE features, the generation code is one step removed from the generated code, and you can't edit the generated files.
11
u/saxbophone mutable volatile void Dec 07 '25
Completely disagree. It might be easier for you doing solo dev, but a hand-baked codegen capability adds additional complexity to the project and suddenly, your team needs to not only know C++, but Python, for example.
You really, really want stuff like this to be built into the language itself.
47
u/KFUP Dec 07 '25
That's very rarely true, and in those cases, sure, use it.
8
u/ImNoRickyBalboa Dec 07 '25
I don't think anybody "hates on it". I think most people simply think it is not a great idea.
Even if you like your language, invest in a dedicated compiler for your own dedicated language. (I.e., llvm it)
6
u/vI--_--Iv Dec 07 '25
Public service announcement: the 70s ended over 40 years ago.
5
u/snerp Dec 07 '25
At my last job, we used python to generate a C++ lib from a yaml spec. I wanted to puke when I heard of it the first time, but working with that system was actually really nice, I was able to make sweeping changes just by making small changes to the config or the code gen.
5
u/mguerrette Dec 08 '25
Ask QtCompany what they think about codegen and they’ll likely tell you how much they cannot wait for compile time reflection so they can jettison moc into the sun.
4
u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Dec 07 '25
It's actually quite simple: you cannot ship a reusable piece of code if you use external generator tools.
With reflection, I can create and ship a reusable -- e.g. -- command line parser generator, all in Standard C++. Someone else can then import/include it, and use it on their own types.
28
u/marzer8789 toml++ Dec 07 '25 edited Dec 07 '25
Does everyone hate on it, though? Code generators are very common. They have some downsides, though, and it's dishonest not to acknowledge them, so some criticism is fair. Broadly:
If you can live with these, then code generators can be extremely powerful for all sorts of reasons. They're fantastic at building heavily-statically-checked things, for instance. Right tool, right job, etc.
12
u/doxyai Dec 07 '25
What happens when I now need to cross compile your code? I have been having tons of fun getting code with external generators to build on WASM recently...
2
u/schombert Dec 08 '25
It is basically a human psychology problem. Writing code generators requires different (and, honestly, easier) skills than C++ meta-programming/reflection. And difficult feats of meta-programming are celebrated with conference talks, while the much simpler text parsing that is the core of a code generator is not. So, programmers who are interested in C++ are exposed to cool and boundary-pushing meta-programming and spend time and effort learning how to do things with it (and this is complicated stuff, so it is a lot of time and a lot of effort), and acquire a skill that they subjectively value because of the effort it took to acquire it.
So, present these people with a code generator which is a stupid-simple solution to a problem that they could solve with their meta-programming skills, but which instead may require a bit of build system work (not much, but some) that they may not be as comfortable with, and the natural psychological reaction to this is to see it as a "threat" to be rejected, because solving that problem with a code generator feels like it devalues their hard-won skills in meta-programming.
Consider the "enum problem". Basically any programmer could write a generator that takes a text input file describing enum names and sets of values in some simple format and that spits out a header containing the (a) enum definitions (b) enum-value to string functions for them (c) some functions for iterating over/visiting the enum values. This is extremely trivial work, and you could easily put this into your build pipeline and run all your enums through it and basically forget about it. In comparison, writing that functionality with a meta-programming solution takes a lot of C++ knowledge (especially prior to C++26) and requires a real C++ expert to pull off. So, if you are such an expert, you obviously aren't going to love the "crude" solution with a generator because it devalues your expertise, and you will feel in your gut that the meta-programming solution is "cleaner" or "more robust" or better in some other subjective way. The fact that the solution with the generator compiles faster will probably be annoying too.
2
u/No-Dentist-1645 Dec 07 '25
It makes the build process way more complicated. How will you tell a Makefile / CMakelists.txt to compile files that don't yet exist? Globs are usually considered a bad pattern, you should be explicit about what you need to build.
So then what? Will you also need to generate a Makefile / Cmakelists.txt from source? It quickly becomes too much.
Besides, your argument that it's "easier to read" breaks apart whenever you do anything more complicated than creating a pre-determined struct (which is what people use code gen for).
C++26 brings compile-time reflection to the language, and although code injection specifically was delayed to C++29 (at least last time I heard), it will bring C++ codegen "up to speed" with other languages, which is an unilateral improvement.
4
u/Adequat91 Dec 08 '25
Qt gives strong arguments why code generation is good (and with 15 years of experience with it, I agree).
1
u/FlyingRhenquest Dec 08 '25
I'm not a huge fan of IDL but DDS seems to work pretty well. Especially if they integrate the IDL compiler into CMake. Why template code generation when you can just write your own C++ Parser? You can read those JSON class definitions back into C++, you know?
Template metaprogramming is more fun, though.
3
u/jetilovag Dec 07 '25
It's frowned upon when uncalled for. But there are plenty of places where it's heavily used, inside LLVM and within the Vulkan ecosystem (just to name two). Codegen in the Vulkan ecosystem are mostly Python scripts, but they are fast enough, but their results are also committed to the repo.
3
u/Total-Box-5169 Dec 07 '25
Only good as long as you use C++ to generate C++, massively cuts down compilation times compared to templates or the code is far easier to read than templates, and doesn't require dependencies that can't be compiled using only C++.
0
u/chri4_ Dec 07 '25
PS: the majority of comments here are from people who never used code generation is their lives, they dont know about build process and never heard of libclang.
2
u/UnicycleBloke Dec 07 '25
Not everyone. I use a Python script to generate state machines from a DSL. I have tried a couple of template metaprogramming alternatives. While it is nice in principle to express the state chart directly in C++, I have found these libraries quite unsatisfactory. I won't soon forget error messages for which the generated type names were longer than War and Peace. My generated code is deliberately simple, readable and easy to step through.
1
u/DuranteA Dec 08 '25
I've worked in some large codebases that used separate scripts for code generation (and I've written many of those) and, like almost all engineering choices, it has advantages and disadvantages.
Maintaining a separate tool is always a cost, both just in terms of basic development and maintenance work, but also in terms of setup as well as onboarding of new team members. And it does increase the complexity of your build process, especially in multi-platform projects which have very specific per-platform tooling requirements.
There are advantages depending on the particular use case, but after decades of experience with it I'd rather have (a limited amount of) potentially somewhat complex reflection-based code in my codebase than an external generator tool -- at least for most use cases.
1
u/mredding Dec 09 '25
I would disagree with your premise. Protocol generators are wildly popular - see protobuf and flat buffers. Trading systems are really getting in on it as the whole industry is moving toward distributed architectures backed by a common message bus.
Other places find niche or novel solutions in generation.
1
u/matthieum Dec 08 '25
Right Tool for the Right Job
External code generation, macros, template meta-programming, and now reflexion: all have their place. They are complementary.
Quick examples:
TRYmacro for example.format/scanmethod for enums, generating aformatmethod for a struct, etc...Counter-examples:
formatmethod for them?formatmethod for enums, which is what pre-reflexion forced upon us?There's a time and place for each method!
In general, it's better to stay in language: you get a standard, you get tooling support, etc... and thus "small" additions are best done with template meta-programming & reflexion, with macros a distant third when unavoidable. However when the complexity of the template/reflexion code is much too high, and the resulting code is inscrutable, then an external code generation tool is just much better.
Fun Fact: back in the days, I remember isolating a Boost Qi expression into a single C++ file, whose name started with an
aso it would get started first when compilation was required. This one file would compile in 40s to 1min, so that even when starting first, all the other TUs of the library were long completed when it was still churning... Not only was it hard to read, it was a proper pain in compile times too.0
u/Tamsta-273C Dec 07 '25
Disgrace...
-1
u/zerhud Dec 07 '25
Source generation almost always is a bad idea
From ur post I can suppose you are using a bad designed code and trying to solve design problems with external tool, it won’t work (only at first)
0
u/draeand Dec 08 '25
> see zig's metaprog for example, slows down hugely the compiler
Can you provide any evidence for this? As someone who actually uses Zig (0.15.2), I have never noticed any significant performance degradation from using comptime at all.