Thought people might be interested.
Blog post: https://herbsutter.com/2017/07/26/metaclasses-thoughts-on-generative-c/
My 10,000 ft view: Seems quite powerful. Feels a lot like procedural macros, but crucially, with access to type and other semantic information for existing declarations (which can then be used to generate new declarations); also more lightweight. This is pretty much my #1 pet language feature I’ve always wanted to see in Rust in some form… but that’s just me.
Two example snippets pulled from the paper:
Basic code injection
constexpr { // execute this at compile time
for... (auto m : $T.variables()) // examine each member variable m in T
if (m.name() == “xyzzy”) // if there is one with name “xyzzy”
-> { int plugh; } // then inject also an int named “plugh”
}
Writing as-if a new ‘language’ feature using compile-time code
$class interface { // see §3.1
constexpr {
compiler.require($interface.variables().empty(),
"interfaces may not contain data");
for... (auto f : $interface.functions()) {
compiler.require(!f.is_copy() && !f.is_move(), "interfaces may not copy or move; consider a" " virtual clone() instead");
if (!f.has_access()) f.make_public();
compiler.require(f.is_public(),
"interface functions must be public");
f.make_pure_virtual();
}
}
virtual ~interface() noexcept { }
};
// User code (proposed C++)
interface Shape {
int area() const;
void scale_by(double factor);
};