Macromancy, Part 2

The Challenge

Given a macro that expands to an arbitrary list of headers, include every header in the list, in order.

#define HEADERS "stdio.h", "stdlib.h", "assert.h"
#include "include_dynamic.h"
// stdio.h, stdlib.h, and assert.h are now included and available.

Progress So Far

The C preprocessor is a powerful tool, but not a particularly versatile one. Rather, it does a few specific things, and people have taken advantage of its workings to compose those capabilities to accomplish their tasks. And sometimes, to do something ridiculous and overengineered…like this.


In section 6.10.2, paragraph 8 of the C11 standard, this example is specifically called out as legal (if implementation-defined):

#if VERSION == 1
  #define INCFILE "vers1.h"
#elif VERSION == 2
  #define INCFILE "vers2.h" // and so on
  #define INCFILE "versN.h"
#include INCFILE

Yikes! Do people really want to do this? If I encountered this in a real program, I’d try to get rid of it—in most cases I figure you can just put the #includes inside the #if and be done with it. You can’t even do any clever tricks like building up a path, at least not portably.

So of course, after telling someone else about this, my next reaction was how to take it further. (Please do not put this in a real program.)

C++ Templates are Turing-Complete

Apologies to anyone reading. These last few weeks are very busy and I do not have time for a legitimate post. Hopefully things will be more regular later…?

So meanwhile, I present this semi-formal proof that the C++ template system is Turing-complete, shown to me by a friend. This means that, among other things, a C++ program that uses templates can (theoretically) take both infinite time and infinite space to compile!

I am not a C++ fan, though it has a few features I appreciate. But give me a nice OO system, with a solid C-language interface…Objective-C does not…

Older Posts

  1. 2009-03-27 Const Correctness

Possibly Related Tags