c - Mixing variables and integer constants in the Boost Preprocessor -


i using boost_pp precompile computations in preprocessor. focusing on application code size extremely important me. (so please don't compiler should or usually that, need control performed @ compile time , code generated). however, want able use same name of macro/function both integer constants , variables. trivial example, can have

#define twice(n) boost_pp_mul(n,2) //..... // somewhere else in code int = twice(5); 

this want to, evaluating

int = 10; 

during compile time.

however, want used at

int b = 5; int = twice(b); 

this should preprocessed

int b = 5; int = 5 * 2; 

of course, can using traditional macros like

#define twice(n) n * 2 

but doesnt want integer constants (evaluating them during compile time).

so, question is, there trick check whether argument literal or variable, , use different definitions. i.e., this:

#define twice(n) boost_pp_if( _is_constant(n), \                               boost_pp_mul(n,2), \                               n * 2 ) 

edit: after way check if constant available @ compile time, , hence argument boost_pp_ functions. realize different people expect preprocessor , general programming recommendations. there no wrong way of programming, please don't hate on question if disagree philosophy. there reason boost_pp library exists, , question in same spirit. might not possible though.

you're trying better left optimizations of compiler.

int main (void) {   int b = 5;   int = b * 2;    return a; // return use , it's not optimized away } 

gcc -o3 -s t.c

 .file "t.c"  .text  .p2align 4,,15 .globl main  .type main, @function main: .lfb0:  .cfi_startproc  movl $10, %eax  ret  .cfi_endproc .lfe0:  .size main, .-main  .ident "gcc: (debian 4.5.0-6) 4.5.1 20100617 (prerelease)"  .section .note.gnu-stack,"",@progbits 

optimizing compiler optimize.

edit: i'm aware don't want hear compiler "should" or "usually" that. however, trying not meant done in c preprocessor; people designed c language , c preprocessor designed work preprocessing token it's basic atom. cpp is, in many ways, "dumb". not bad thing (in fact, in many cases makes useful), @ end of day, preprocessor. preprocesses source files before parsed. preprocesses source files before semantic analysis occurs. preprocesses source files before validity of given source file checked. understand not want hear parser , semantic analyzer should handle or does. however, reality of situation. if want design code incredibly small, should rely on compiler it's job instead of trying create preprocessor constructs work. think of way: thousands of hours of work went compiler, try reuse of work possible!


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -