Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any c++ metaprogramming alternatives besides templates?

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?

like image 485
kamziro Avatar asked Jan 11 '11 20:01

kamziro


People also ask

Are templates metaprogramming?

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".

Why is template metaprogramming useful?

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.

Does C have template?

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.

Does template support generic programming?

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.


1 Answers

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.

like image 85
Edward Strange Avatar answered Nov 01 '22 14:11

Edward Strange