Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preprocessor macro expansion to another preprocessor directive

Initially I thought I needed this, but I eventually avoided it. However, my curiosity (and appetite for knowledge, hum) make me ask:

Can a preprocessor macro, for instance in

#include "MyClass.h"

INSTANTIATE_FOO_TEMPLATE_CLASS(MyClass)

expand to another include, like in

#include "MyClass.h"

#include "FooTemplate.h"
template class FooTemplate<MyClass>;

?

like image 972
moala Avatar asked Aug 11 '09 18:08

moala


People also ask

What is macro expansion in preprocessor?

The LENGTH and BREADTH are called the macro templates. The values 10 and 20 are called macro expansions. Advertisements. When the program run and if the C preprocessor sees an instance of a macro within the program code, it will do the macro expansion. It replaces the macro template with the value of macro expansion.

What does the macro Tablesize expand to?

TABLESIZE is expanded first to produce BUFSIZE, then that macro is expanded to produce the final result, 1024.

Which C preprocessor directive is used for macro substitutions?

Macro Substitution is a process where an identifier is replaced by a predefined string or a value. "#define" is the Macro Substitution directive in C.

What do you mean by macro expansion in C?

Macro definition and Expansion A macro consists of name, set of formal parameters and body of code. The use of macro name with set of actual parameters is replaced by some code generated by its body. This is called macro expansion.


1 Answers

I believe that cannot be done, this is because the pre-processor is single pass. So it cannot emit other preprocessor directives.

Specifically, from the C99 Standard (6.10.3.4 paragraph 3):

3 The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one, ...

Interestingly enough, This is why the unary _Pragma operator was added to c99. Because #pragma could not be emited by macros, but _Pragma can.

like image 84
Evan Teran Avatar answered Oct 07 '22 11:10

Evan Teran