Errors, bugs, questions - page 2864

 
Andrey Khatimlianskii:

So why doesn't it work?

Most likely, this approach doesn't work anywhere. Because there is a substitution going on where the VALUE on the right is undefined.

#define  VALUE (VALUE*2)
 
fxsaber:

This approach probably doesn't work anywhere. As there is a substitution going on where the VALUE on the right is undefined.

And it's evil to change someone else's macro substitution. All the more so, if these are predefined environment macros. It doesn't matter where in the end the overriding macro will be, after all the project's files are assembled. It's better to define your own _VALUE macro.
 
Vladimir Simakov:
Yes, and it is evil to change someone else's macro substitution.

Without this evil, it would not have happened.

 
Vladimir Simakov:
And it's evil to change someone else's macro substitution. Especially if they are predefined environment macros. It doesn't matter where in the end, after all the files of the project are assembled, the overriding of the macro will end up. You'd better define your own _VALUE macro.
May I ask why you can't use the VALUE value assignment to a global variable and multiply it and substitute it in the final define?
 
Valeriy Yastremskiy:
May I ask why you can't use a VALUE assignment to a global variable and multiply it and substitute it in the final define?

You can, but then the meaning of macro substitution is lost (substitute values in the code). All hope remains on the compiler, that it converts const type (have you, by the way, defined the variable type?) to c++ constexpr type

But otherwise, yes, you can.

#ifdef  MACROS
   const int gValue=2*VALUE;
   #undef  VALUE
   #define  VALUE gValue
#endif
 
Vladimir Simakov:

But then, yes, you can.

It won't work with that.

void Func( int = VALUE ) {}
 
fxsaber:

It won't work with this one.

Right. Once again, let's ask constexpr to make it like this
constexpr auto x = 100;

void Foo(int _x = x) {
    std::cout << _x<<std::endl;
}

int main() {
    Foo();
    return 0;
}
 
fxsaber:

This approach probably doesn't work anywhere. As there is such a substitution going on, where the VALUE on the right is not defined.

Apparently, I don't understand how the compiler works at all. I imagined that strings are parsed 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
 
Andrey Khatimlianskii:

Apparently, I don't understand how the compiler works at all. I imagined that strings are analysed sequentially. So there is no undefined value on the right:

Works top-down like text replacement. I.e. "TMP" will be replaced by "VALUE".

 
Andrey Khatimlianskii:

Apparently, I don't understand how the compiler works at all. I imagined that strings are analysed sequentially. So there is no undefined value on the right:

It's not the compiler - it's the preprocessor)

#define  VALUE (TMP*2)

turns into

#define  VALUE (VALUE*2)

, and your VALUE is undefined.