Errors, bugs, questions - page 2866

 

Everything is possible, only one macro needs to be added

#define  VALUE 10

MACRO( VALUE, VALUE2 )

#define  VALUE   VALUE2*2

void f( int i = VALUE ) { Print( i ); }
void OnStart()
{
        f();
}

Result: 20

 
A100:

Everything is possible, only one macro needs to be added

Result: 20.

That's very good. I'm not good at guessing puzzles.

 
Vladimir Simakov:

Sorry, I got confused while I was trying to explain myself)))

One more time:

At the time of the second VALUE definition, the VALUE macro is not defined, so VALUE is defined as

, because TMP was and still is defined by VALUE.

But TMP, after the second VALUE definition, is expanded to

(Something like this))

The preprocessor just fills in what it has and it doesn't matter how and where it is defined. This is why you have to be careful with it. Example:

, and now let's add an evil thing to the function, namely a side effect

And this is just an inscription, but what if the deposit depends on this function?

funny) when you get it)

#define  VALUE 10        
#define  TMP VALUE       
#undef  VALUE     

got it right, after undoing the substitution, the first line is no longer 10 and the second TMP is VALUE, but not 10. I.e. substitution is far from assignment.

 
Valeriy Yastremskiy:

funny) when you get it)

got it right, after undoing the substitution, the first line is no longer 10 and the second TMP is a VALUE, but not 10. I.e. a substitution is far from an assignment.

Yes. A substitution is exactly a substitution, one-to-one. But when the VALUE macro is defined in your code (not to be confused with the preprocessor directive), the preprocessor further expands TMP->VALUE->10 and if not, TMP->VALUE. In this case, the preprocessor directives are not involved in the code itself, they are no longer there when compiled. Example:

#define  VALUE 10
#define  TMP VALUE

void OnStart()
{
   Print(TMP); //10
if (false){
   #undef  VALUE
   #define  VALUE 20
   Print(TMP);}
else Print(TMP); //20
}
 
Vladimir Simakov:

Yes. Substitution is exactly substitution, one-to-one. Simply, when the VALUE macro is defined during substitution in your code (not to be confused with preprocessor directives), the preprocessor will expand TMP->VALUE->10, and if not, TMP->VALUE. In this case, the preprocessor directives are not involved in the code itself, they are no longer there when compiled. Example:

Yes, if you comment out the second VALUE substitution 20, the VALUE variable declaration will be gone and the compiler will not see it and will scold)

 
A100:

Everything is possible, only one macro needs to be added

Result: 20

I give up)))

How is it defined?

MACRO

?

 
Vladimir Simakov:

I give up)))

How certain

?

So quickly? Not all the specialists have joined in yet... We'll wait a week.

Hint: this also works (but the solution is slightly different)

#define  VALUE1 10
#define  VALUE2 5

MACRO2( VALUE1, VALUE12 )
MACRO2( VALUE2, VALUE22 )

#define  VALUE1  VALUE12*2
#define  VALUE2  VALUE22*3

int f1( int i = VALUE1 ) { return i; }
int f2( int i = VALUE2 ) { return i; }
void OnStart()
{
        Print(f1(),":",f2());
}

Result: 20:15

 
Vladimir Simakov:

because TMP was, and still is, defined by VALUE.

This is where the "top down" refutation comes in.

Otherwise TMP would not have been "as defined, so it remains defined", but would have been previously replaced by 10 (to which VALUE is replaced).

So the preprocessor is not processing the code line by line. What remains to be figured out is how.


@A100, if you don't mind, in a nutshell.
Not interesting enough to google and read, but enough to ask.

What is wrong with my logic?

I imagined that the strings are analysed sequentially. So there is no undefined value on the right:

#define  VALUE 10       // VALUE = 10
#define  TMP VALUE      // TMP = 10
#undef  VALUE           // VALUE = EMPTY
#define  VALUE (TMP*2)  // TMP = 10, следовательно VALUE = 20
 
A100:

Hint: this also works (but the solution is slightly different)

So identical in action to MACRO and MACRO2.

 
Andrey Khatimlianskii:

What is wrong with my logic?

#define  VALUE 10       // VALUE = 10
#define  TMP VALUE      // TMP = 10 VALUE
#undef  VALUE           // VALUE = EMPTY
#define  VALUE (TMP*2)  // TMP = 10 EMPTY, следовательно VALUE = 20 EMPTY
Reason: