1

how can I make this example code work?(In C or C++)

The cout is just for example.I want evaluate the correct decremented number

#define PRINT_1 std::cout<<"One : " <<1;
#define PRINT_2 std::cout<<"Two : " <<2;

#define DEC_AND_PRINT(number) PRINT_##number-1

When I call DEC_AND_PRINT(3) , I expect this:

DEC_AND_PRINT(3)  PRINT_##(3-1) -> PRINT_2 -> std::cout<<"Two : "<<2

But the compiler give-me an error :

GCC : error: 'PRINT_3' was not declared in this scope...

GCC : note: in expansion of macro 'DEC_AND_PRINT' DEC_AND_PRINT(3)

How I can decrement the argument?

Basically, I'm trying to make a macro function get a number and call another macro function in syntax _name_of_macro_decremented_number.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    `PRINT_1 std::cout<<"One : " <<1;` will not work in C. Dropping C tag. – chux - Reinstate Monica Oct 04 '17 at 03:23
  • 2
    You can't. The preprocessor can't be used like this. – user207421 Oct 04 '17 at 03:24
  • Thanksx..But there any way to an macro evaluate an arithimetic expression and use the result to call the macro name __## computed value? – Diego Teixeira de Souza Oct 04 '17 at 03:31
  • You can't make it work in C; `std::cout <<` notation is inherently C++ and only C++. – Jonathan Leffler Oct 04 '17 at 03:48
  • This is probably a duplicate of ["How, exactly, does the double-stringize trick work?"](https://stackoverflow.com/questions/2751870/how-exactly-does-the-double-stringize-trick-work) (or some other similar question). You need to use an extra layer of macro-indirection to evaluate `3-1` to its final value of `2`. – Cornstalks Oct 04 '17 at 03:53
  • @DiegoTeixeiradeSouza This being the preprocessor, you need to be _crystal clear_ and _specific_. It's impossible to have `EVAL(11+22*3)` expand to `77` but `EVAL(11+3*3)` expand to `20`. It's possible but involved to have `ADD(11,MUL(22,3))` expand to `77`. It's fairly straightforward to simply make your macro work (have `DEC_AND_PRINT(3)` evaluate `PRINT_2`). Which do you need? – H Walters Oct 04 '17 at 06:15
  • @Cornstalks Indirection won't help; macros don't evaluate expressions. _Conditional directives_ (`#if`/`#elif`) can, but not macros. – H Walters Oct 04 '17 at 06:27
  • @HWalters: Ah crap, you're right. I was mixing up macro expansion and macro evaluation. Thanks for pointing that out. – Cornstalks Oct 05 '17 at 03:35

1 Answers1

0

Thanksx for all.

There is no way to do what i wanna do.The Preprocessor cannot evalute an expression an use the result in an concatenation ##.

So im change the code to use recursion , and write all combinations in hand,like:

CALL DefinePointer(T)

DefinePointer_1(T)

Define T * -> Call DefinePointer_2( T*)

                DefinePointer_2( T*)
                Define T**  -> Call DefinePointer_3(T**)
                                    DefinePointer_3(T**)
                                  Define T*** -> Call DefinePointer_4(T****)                     

This is only a piece of code, but there is a lot more. I had to write a lot but in the end it worked, and it was possible to write algorithms and data structures for any kind of data.

  • 1
    Actually, the _preprocessor_ can [evaluate an expression and use the result in a concatenation](http://coliru.stacked-crooked.com/a/cf4fa3f0c6e2493f). You just can't make a _macro_ that evaluates an expression. – H Walters Oct 05 '17 at 01:05