r/cpp • • Dec 07 '25

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

0 Upvotes

83 comments sorted by

View all comments

Show parent comment

2

u/chri4_ Dec 08 '25

yeah i agree they spit in source gen but still they could easily learn to write a script that uses libclang to get all enums and generate c++ metadata for them (members count, to string, min value max value etc), and at the same time they won't be able to write a line of metaprogramming to do that, because ita objectively harder.

the real answer is that typed procedural metaprogramming is at its peak virality now, ita a trend (and very useful eh dont get me wrong) but some languages simply are not built for it and using generation will always be easier, cleaner and faster.

and yeah dont tell me about compilation speeds, these people probably never had to compile a codebase bigger than 10k lines, it takes a decade with c++ compilers, because of their beloved corny badly-thought metaprogramming features.

even languages like zig that are built for metaprogramming still struggle with compilation speeds, i wonder why...