re: Why C++ (is fun to me): template metaprogramming VIEW POST

re: The tool of choice I would recommend to do that would be swig, not templates. Did you consider that?

Never heard of that before, thank you! Looks like that should be the tool of choice if you are targeting one of the supported languages and want to wrap a lot of code/functionality.

Might be a bit heavyweight in some other cases since it is (as far as I can see) an external tool with its own specific syntax and all. If you don't want to leave the scope of your compiler, or you are targeting an interface (possibly not even a scripting language, but just an internal interface in your project) which is not supported by SWIG, templates may still be of use.


Yes, the target language must be supported, but most common languages are already. If not, you would get your hands dirty providing one, and swig is not simple in that regard.

Apart from that, configuring swig can be as simple as defining your module name and specifying your include file: thus it can be as short as 3 lines of swig source. That, of course, depends on your interface design. If it gets too fancy, you have to give swig some hints in how to wrap. That's mostly because as developer you do not have to give all information about how to use a function to the compiler. One simple example for this is a pointer argument: is it an input to the function, an output or both?

And yes, swig syntax is not trivial if you leave the most common cases.

Regarding the compiler scope: That is also possible. Swig generates source code for C or C++. All you need is an additional wrapper-generate step, which should be handled in Makefile (or whatever build tool you use). And you can generate the wrapper and provide it, thus that one can compile without needing swig, e.g. in order to install. I do that, and I have automake files detecting if swig is available. In that case, the wrapper gets regenerated, otherwise I use the provided one. Or I might have misunderstood you...

Having said all this, I still find your article interesting and valuable. Having alternatives is always useful.

Ah, I see! Thanks for the additional information. I will have to take a closer look at swig the next time I need such a solution.

code of conduct - report abuse