#define statements within a namespace

前端 未结 5 1556
旧巷少年郎
旧巷少年郎 2021-02-01 12:34

If I have a #define statement within a namespace as such:

namespace MyNamespace
{
  #define SOME_VALUE 0xDEADBABE
}

Am I correct in saying that

5条回答
  •  孤城傲影
    2021-02-01 13:00

    If, for some reason, you couldn't change to the second case, I'm pretty sure you would have to take care to compile MyNamespace into its own object and link the objects separately (or possibly just run the preprocessor on that single namespace by itself). The C++ preprocessor should take that #define statement and essentially do a string replace anywhere that SOME_VALUE is seen in the source code. Hence if the preprocessor is not aware of the #define, SOME_VALUE cannot be replaced in another source file.

提交回复
热议问题