Note that there are some explanatory texts on larger screens.

plurals
  1. POAre there any c++ metaprogramming alternatives besides templates?
    text
    copied!<p>I have been using metaprogramming quite a lot, but sometimes the combination of c macros and templates are just not enough.</p> <p>I suppose the drawback would potentially be lack of cross-platform compatibility if the metaprogramming platform is only for, say, linux etc.</p> <p>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..</p> <p><strong>edit</strong>: here's an example on something I've been working on.</p> <p>Suppose I have a generic class for saving/loading files into and from buffers. Let's call it FilePack.</p> <p>I have a define macro, which looks like</p> <pre><code> defineFilePack(BaseClass, "code-a") </code></pre> <p>It basically creates a class called "BaseClassPack", which is defined to be a subclass. Below is that thing. </p> <pre><code>class FilePack{ public: char * thebuffer; int bufsize; string packcode; // and constructors etc FilePack(const string&amp; thecode, int bufsize); void operator=(FilePack&amp; rhs); void saveToFile(const string&amp; filename); void loadFromFile(const string&amp; filename); // .. and all the function you'd expect to see in a class like this }; // the person details class PersonDetails{ public: solidstring&lt;64&gt; 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&amp; get(){ return *(PersonDetails*)getBuffer(); } // and a lot more convenience function }; </code></pre> <p>Now, there's actually an in-built check by FilePack's constructor that the declared code matches with the size, using a global map.</p> <p>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.</p> <p>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.</p> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload