News for hackers. Stuff that matters.
Re:Templating in C by arquivo on Saturday February 28, 2015 @11:46AM (#9124174)
Why complicate with multiple files, functions, defines and includes; here it is in one file with a single define:

http://pastebin.com/uWSs7ca7

We can improve the macro further by removing the unnecessary if statement and the index number:

http://pastebin.com/vPTTYPpe

Re:Templating in C by ux on Saturday February 28, 2015 @04:18PM (#9125094)
These solutions are what is presented at the beginning of the article, basically. They suffer from a maintenance and readability issue (most editors will disable syntax in the macro scope, you have to mess with the '\', ...).

In this particular case it's not exactly a problem, but when you reach a 100+ lines macro, it becomes one.

It's a bit like maintaining pixel shaders in strings, it pretty quickly becomes a huge PITA.

Now if you read carefully the post, you will notice that I'm not encouraging the latest multiple files method, it is just presented because it's a common pattern and has its use cases.

I think what is described in "Mixing full functions mechanism and macros" would be more appropriate for the example you picked. Again, you have to assume that the function is going to grow, and it needs to be editable.

Re:Templating in C by arquivo on Sunday March 01, 2015 @03:57AM (#9126480)
Re:Templating in C by ux on Sunday March 01, 2015 @04:09AM (#9126494)
Yes, that's what I presented. Sometimes though you need a function because you have a function pointer tab for each feature (foo, bar, baz), that's why I wrapped them to show the mechanism of inlining.
Re:Templating in C by alxmdev on Saturday February 28, 2015 @02:36PM (#9124741)
I prefer the single file solution too. I suppose it comes down to how much having escape markers on every line of the function implementation bothers you, and if your favorite code editor does full syntax highlighting in such a case.
Re:Templating in C by arquivo on Saturday February 28, 2015 @02:56PM (#9124814)
There is also a version with a normal function:

http://pastebin.com/Wesjzs1A

Solution in the article is overly convoluted.

Re:Templating in C by alxmdev on Saturday February 28, 2015 @03:16PM (#9124875)
Yes, but that is now a worker function that handles every if(n == x) case explicitly in the body, not something you need the preprocessor for anymore. Nothing against it, but it is a different design.
Re:Templating in C by arquivo on Saturday February 28, 2015 @03:19PM (#9124884)
I copied the function from the article, they are identical. This was done intentionally to show how convoluted the article is.
Re:Templating in C by alxmdev on Saturday February 28, 2015 @03:40PM (#9124966)
I see. I was thinking in the context of your second pastebin link and the evil_template.c snippet in the article, where the functions are actually generic (and in which case you can't avoid the function being declared as a macro unless you go the multiple-includes route).