I have been using metaprogramming quite a lot, but sometimes the combination of c macros and templates are just not enough.
I suppose the drawback would potentially be lack of cross-platform compatibility if the metaprogramming platform is only for, say, linux etc.
So yeah, is there such thing available right now, besides templates? Google search for metaprogramming is dominated by template metaprogramming, so it’s hard to find right now..
edit: here’s an example on something I’ve been working on.
Suppose I have a generic class for saving/loading files into and from buffers. Let’s call it FilePack.
I have a define macro, which looks like
defineFilePack(BaseClass, "code-a")
It basically creates a class called “BaseClassPack”, which is defined to be a subclass. Below is that thing.
class FilePack{
public:
char * thebuffer;
int bufsize;
string packcode;
// and constructors etc
FilePack(const string& thecode, int bufsize);
void operator=(FilePack& rhs);
void saveToFile(const string& filename);
void loadFromFile(const string& filename);
// .. and all the function you'd expect to see in a class like this
};
// the person details
class PersonDetails{
public:
solidstring<64> name;
int age;
DateTime birthday;
// .. yada yada yada
};
defineFilePack(PersonDetails, "psd")
// the above creates the following class
class PersonDetailsPack : public FilePack{
public:
PersonDetailsPack():
FilePack("psd", sizeof(PersonDetails)){ // etc
}
PersonDetails& get(){
return *(PersonDetails*)getBuffer();
}
// and a lot more convenience function
};
Now, there’s actually an in-built check by FilePack’s constructor that the declared code matches with the size, using a global map.
Right now I’m stumped on how to do that using template metaprogramming, which is actually a good fit for it because all of these filepack codes are declared inside the source file. Sure, someone can probably make their own FilePack in run-time, but that’s besides the point.
Another thing that metaprogramming could help with here is to support loading different versions of FilePack. Suppose I had to update the PersonDetails class.. I just make a new class, use some kind of metaprogramming to declare the inheritance, and magically make FilePack know so that when it’s loading an old version of PersonDetails it can call the conversion function, or whatever.
Also, you’re welcome to comment on that architecture, and I’m keen to hear any comments about it, but it might be a bit off-topic?
You can also metaprogram with the preprocessor.
You could consider using special purpose preprocessors to generate code as “metaprogramming” as well. Then you could include things like lex/yacc and the Qt MOC.