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?
No. Not all usages of templates, in C++, are metaprogramming. Obviously it's a question of definitions but, in C++, "metaprogramming" is synonymous of "compile-time computation".
It's faster because the modified (perturbed) expression tree is then available to the optimizer at the same level as the rest of your code, so you get the full benefits of optimization, both within your expressions but also (where possible) between your expressions and the surrounding code.
The main type of templates that can be implemented in C are static templates. Static templates are created at compile time and do not perform runtime checks on sizes, because they shift that responsibility to the compiler.
Generic programming is a key paradigm for developing reusable software components. The inherent support for generic constructs is therefore important in programming languages. As for C++, the generic construct, templates, has been supported since the language was first released.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With