Saturday, February 7, 2009

Fabulous abuse of the C Preprocessor

From an open source kernel extension I was making some changes to:

// warning: egregious abuse of preprocessor follows!
#define private public
#define protected public
#include <IOKit/hidsystem/IOHIDSystem.h>
#undef private
#undef protected


I really just have no words...

3 comments:

Anonymous said...

Will this even work?

What about private sections that don't use the private keyword?

class Rodarmor {
int isPrivate
}

Sorry Casey, just curious and being a jerk

Casey said...

First, I should just mention that I'm not the author of this excellent little snippet of code, I just stumbled onto it :-)

But yeah, you're right, if the keyword doesn't appear, then there's no way for the preprocessor to replace it.

coaxis said...

in which case, depending on compiler, the author probably should've specified something like -fno-access-control