0

Hi I've a problem with my cpp macros:

#define DATA_TYPE_B uchar    
#define DATA_TYPE_A DATA_TYPE_B    //line 2
#undef DATA_TYPE_B    
#define DATA_TYPE_B double    
DATA_TYPE_A A;    
DATA_TYPE_B B;    

this wil give me

double A;
double B;

instead of

uchar A;
double B;

because line 2 doesn't expand DATA_TYPE_B, how can i force that expansion?

Sur3
  • 23
  • 1
  • 5
  • 1
    This seems like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the actual problem you are trying to solve? – wally Dec 18 '17 at 19:37
  • 3
    Please, use a type alias instead of a macro. – StoryTeller - Unslander Monica Dec 18 '17 at 19:37
  • DATA_TYPE_A is not used by the compiler until it is defined as double. Why are you redefining DATA_TYPE_B? – KeithSmith Dec 18 '17 at 19:38
  • @KeithSmith My actual code uses it also before the redefinition, I just simplified the problem. – Sur3 Dec 18 '17 at 19:55
  • @StoryTeller redefinition with typedef doesn't work, is says conflicting types, because I only can do this redefinitions in a global scope. – Sur3 Dec 18 '17 at 20:09
  • @Sur3 - That's not a proposal that's meant to "fix" your issue. It's a proposal for you to stop abusing macros and use the type system. "Fixing" your issue would involve you not even trying to redefine a type. – StoryTeller - Unslander Monica Dec 18 '17 at 20:12
  • @StoryTeller well the redefinition makes sense in our case, as it is glue code for gerneric OpenCL kernels. – Sur3 Dec 18 '17 at 20:29
  • I found https://stackoverflow.com/questions/34756512/how-to-rename-a-c-preprocessor-macro thats pretty similar to my question, they say there is no solution. :-/ – Sur3 Dec 18 '17 at 21:29

4 Answers4

2

When the preprocessor sees the use of DATA_TYPE_A it replaces it with DATA_TYPE_B, and then it replaces DATA_TYPE_B with its definition, which is double. It doesn't keep notes about what the definition of DATA_TYPE_B used to be.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • Yes I understand what's happening, but how can I force DATA_TYPE_A to get the value of DATA_TYPE_B before the redefinition? – Sur3 Dec 18 '17 at 19:52
  • To get the previous definition, don't redefine it until after you've used it. – Pete Becker Dec 18 '17 at 20:07
  • Thats not possible because it is some kind of pipeline where DATA_TYPE_B is the output data type of the previous step of the pipeline and DATA_TYPE_A is the input to the next step, thats why DATA_TYPE_A needs to get the old DATA_TYPE_B value. – Sur3 Dec 18 '17 at 20:13
1

you say to the compiler: everything with DATA_TYPE_A replace with DATA_TYPE_B and then everything with DATA_TYPE_B replace with double

Angen
  • 400
  • 2
  • 10
  • Yes I understand what's happening, but how can I force DATA_TYPE_A to get the value of DATA_TYPE_B before the redefinition? – Sur3 Dec 18 '17 at 19:53
  • place the code where you need it before redefinition, and I do not recommend to do such redefinition from one macro B to another macro A, it is confusing for reader of the code. When you need B to be something else like A just define it that way. – Angen Dec 18 '17 at 19:55
  • Actually in our project it is not confusiong at all, and it is not possible to place it before the redefinition. – Sur3 Dec 18 '17 at 20:10
0

Take a look at #pragma push_macro/#pragma pop_macro (originally limited to MSVC but apparently now supported by both gcc and clang), they allow you to temporarily redefine macro values then revert to the prior value.

Unfortunately macro values are not evaluated until actual expansion so in the case of:

#define macroA float
#define macro B macroA
#undef macroA
#define macroA double
macroB var;

'var' simply is going to expand to double.

But with a compiler that supports push_macro:

#define macroA float
#define macroB macroA
#pragma push_macro("macroA")
#undef macroA
#define macroA double
macroB var1; // var1 is a double
#pragma pop_macro("macroA")
macroB var2; // var2 is a float
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
0

I found a possible solution to my problem replacing line 2 by something like:

#if DATA_TYPE_B==uchar
 #define DATA_TYPE_A uchar
#elif DATA_TYPE_B==float
 #define DATA_TYPE_A float
#elif DATA_TYPE_B==uint
 #define DATA_TYPE_A uint
#else
 #error Unknown DATA_TYPE_B
#endif

That is a bit ugly and i wish there was just some expansion or evaluation macro though, think I have to write such a macro using such if statements. :-/
Also these camparisons can't be made directly but I have to replace the strings by integers it seems.. :-/

Sur3
  • 23
  • 1
  • 5