C++26: #embed
7 points by raymii
7 points by raymii
It's worth reading about JeanHeyd Meneide's quest to get this added. Seriously, what an absurd saga. We should all be grateful for heroes like this, but also, man, the process is broken when a feature this obviously valuable needs this kind of heroics.
The problem is, it isn’t obviously valuable in C. Very few programs need to embed another file in the source, and for those that do there are existing solutions. xxd can generate a header file with a byte array in it (parsing this one token per byte is not fast for large things, but for a few KiBs it’s almost instant). You can embed binaries directly from linker scripts and expose symbols, which is fast but means you can’t use sizeof on the result. There are few places where neither of these approaches is sufficient, so it’s a new feature with questionable value.
Worse, a feature like this is useful in C++, but not this feature. It’s really nice to be able to pull in something from another file and use it in a template, with consteval members, or feed it to reflection. With reflection and consteval, you can start to do really exciting things like have a template that takes a filename as an argument and is instantiated to parse that file and generate the code for it, for example generating proxies for remote objects based on an IDL file, without any additional tools. And that’s great, except the evaluation of #embed is in the wrong phase for use from templates because it’s already expanded by the time you get there. And that’s why C++29 is getting a different mechanism, which can reuse some of the logic in the compiler, but which is not simply a wrapper around #embed.
xxd can generate a header, but xxd is part of vim. Depending on a text editor because it happens to bundle a script to convert a file to a C byte array feels ... unnecessary, so I always make my own little Python script to do the same (because I usually already depend on Python through Meson and other build scripts).
It's totally an incidental thing, and if xxd was just its own tool or part of util-linux or coreutils or something it'd be a different topic. But it happens to be part of vim, so it's not that useful.
This is an incredibly useful language feature and I'm glad it's been added!
In my compiler, jank, I'm using it for two things:
However, I had no idea that the #embed parameters existed until this article, so thank you for exhaustively covering those! :)