preprocessor-abuse

Including one C source file in another?

Is it OK (or even recommended/good practice) to #include .c file in another .c file? What happens when they are included in a project file? ...

What is the worst real-world macros/pre-processor abuse you've ever come across?

What is the worst real-world macros/pre-processor abuse you've ever come across (please no contrived IOCCC answers *haha*)? Please add a short snippet or story if it is really entertaining. The goal is to teach something instead of always telling people "never use macros". p.s.: I've used macros before... but usually I get rid of the...

How do I mark code with side effects?

I'm working on a project on an 8051 where every byte counts. As such, I am using some global variables where I normally wouldn't. The normal method of passing pointers into a function adds too much overhead here. I have a number of functions that use single bit variables (a compiler specific extension to C) to signal the outcome of a ...

Variadic recursive preprocessor macros - is it possible?

I've run into a little theoretical problem. In a piece of code I'm maintaining there's a set of macros like #define MAX_OF_2(a, b) (a) > (b) ? (a) : (b) #define MAX_OF_3(a, b, c) MAX_OF_2(MAX_OF_2(a, b), c) #define MAX_OF_4(a, b, c, d) MAX_OF_2(MAX_OF_3(a, b, c), d) ...etc up to MAX_OF_8 What I'd like to do is replace them wi...

Functional programming in C with macro "Higher Order Function" generators

Pay attention carefully because this is a hell of a question ;-) I want to use template functions for generic collection actions (like search, foreach, etc.) in C while maintaining compiler static type checking. It is fairly straightforward while you're using simple callbacks like in this example: #define MAKE_FOREACH(TYPE)\ void forea...

What is #nomacros (EP003), and is it alive?

The Evolution WG Issues List of 14 February 2004 has ... EP003. #nomacros. See EI001. Note by Stroustrup to be written. In rough (or exact) terms, what is #nomacros, and is it available as an extension anywhere? It would have been a useful diagnostic tool in a recent project involving porting thousands of files of 1995-vintage C...

Given text in a #define, can it somehow be passed to a template?

Say I have a macro, FOO(name), and some template class Bar<> that takes one parameter (what type of parameter is the question). Everytime I call FOO with a different name, I want to get a different instantiation of Bar. The Bar<> template doesn't actually need to be able to get at the name internally, I just need to be sure that differen...

Javascript-friendly preprocessor dilemma.

I've been working on a (nearly finished) Javascript project for a little over 14 months now. The project started out as a hack I expected to finish overnight, but over time the Javascript part has grown up to be 68 separate files and 10,314 non-empty lines, sadly currently dependent on the C preprocessor for building. It's hard to expla...

Where do I put all these function-like #defines, in C?

I'm working with an embedded system, and I'm ending up with a ton of HW-interfacing #define functions. I want to put all of these into a separate file (for OOP-ness), but I don't know the best way to #include that. Do I just put them all into a .c file, then include that? Seems silly to put these in a .h file. ...

How do you perform macro expansion within #ifdef?

Hi all, I have some fairly generic code which uses preprocessor macros to add a certain prefix onto other macros. This is a much simplified example of what happens: #define MY_VAR(x) prefix_##x "prefix_" is actually defined elsewhere, so it will be different each time the file is included. It works well, but now I have some code I ...

C++ preprocessor #define-ing a keyword. Is it standards conforming?

Help settle the debate that's going on in the comments at this question about bool and 1: Can a standards-conforming C++ preprocessor allow one to use #define to redefine a language keyword? If so, must a standards-conforming C++ preprocessor allow this? If a C++ program redefines a language keyword, can that program itself be standard...

What are the lengths/limits C preprocessor as a language creation tool? Where can I learn more about these?

In his FAQ @ http://www2.research.att.com/~bs/bs_faq.html#bootstrapping, Bjarne Stroustrup says: To build [Cfront, the first C++ compiler], I first used C to write a "C with Classes"-to-C preprocessor. "C with Classes" was a C dialect that became the immediate ancestor to C++... I then wrote the first version of Cfront i...

Remove extra junk from C preprocessor?

I'm trying to use the C preprocessor on non-C code, and it works fine except for creating lines like this at the top: # 1 "test.java" # 1 "<built-in>" # 1 "<command-line>" # 1 "test.java" The problem is that these lines aren't valid in Java. Is there any way to get the preprocessor to not write this stuff? I'd prefer not to have to ru...

Purging Preprocessing Macros

Hi! This is an odd problem, so I have to provide a bit of background. I have a C++ project that I'm working on that I want to clean up a bit. The main issue that I'm dealing with that makes me want to barf is the massive abuse of preprocessor macros that are used in a core component of the project. There's a file with a bunch of #define...

Struct initialisation through macro overuse

Hi, I've got some structs to initialise, which would be tedious to do manually. I'd like to create a macro that will help me with it... but I'm not sure C preprocessor is good enough for this. I've got structs which represent menus. They consist of function pointers only: typedef uint8_t (*button_handler) (uint8_t); typedef void (*peda...

Possible to #if or #ifdef based on preprocessor-generated "thing"

I'm trying to engage in some C-preprocessor-only templating efforts in order to type-specialize some code. I've tried to boil it down a bit, so this example seems trivial and pointless, but the real challenge is getting the "include" blocking. Say I have a "template" file, that gets #included from other source files that define T_ELEME...

Getting boost::function arity at compile time?

I need to make a decision in a BOOST_PP_IF statement based on the arity (parameter count) of a boost::function object. Is this possible? boost::function_types::function_arity does what I'm looking for, but at runtime; I need it at compile time. ...

Replace a string in a macro variable?

I have a macro where I pass in an argument and use that define a new variable based on the name of the input: #define DO_X(x) char _do_x_var_ ## x; /* other things */ The problem is if I pass in a struct variable, it breaks: DO_X(some_struct->thing) becomes: char _do_x_var_some_struct->thing; /* other things */ EDIT: What I want...