#define statements within a namespace

前端 未结 5 1566
旧巷少年郎
旧巷少年郎 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条回答
  •  猫巷女王i
    2021-02-01 13:19

    I completely agree with the suggestions on the use of constants and the scope being unlimited for #defines.

    However, if you do have to use preprocessor #define lines, please cover them up correctly for the expected scope,

    namespace MyNamespace
    {
      #define SOME_VALUE 0xDEADBABE
      // your code
      #undef SOME_VALUE
    }
    

    Why #defines?
    I know one case where an embedded platform did not support constants in the code.
    There was no way to initialize them...
    It always helps to be more readable.

提交回复
热议问题