• <i id='9BKCo'><tr id='9BKCo'><dt id='9BKCo'><q id='9BKCo'><span id='9BKCo'><b id='9BKCo'><form id='9BKCo'><ins id='9BKCo'></ins><ul id='9BKCo'></ul><sub id='9BKCo'></sub></form><legend id='9BKCo'></legend><bdo id='9BKCo'><pre id='9BKCo'><center id='9BKCo'></center></pre></bdo></b><th id='9BKCo'></th></span></q></dt></tr></i><div id='9BKCo'><tfoot id='9BKCo'></tfoot><dl id='9BKCo'><fieldset id='9BKCo'></fieldset></dl></div>

      • <bdo id='9BKCo'></bdo><ul id='9BKCo'></ul>
    1. <tfoot id='9BKCo'></tfoot>
    2. <legend id='9BKCo'><style id='9BKCo'><dir id='9BKCo'><q id='9BKCo'></q></dir></style></legend>

      <small id='9BKCo'></small><noframes id='9BKCo'>

        如何测试预处理器符号是否已#define'd 但没有值?

        时间:2023-06-03
            <tbody id='E4oBq'></tbody>

        1. <i id='E4oBq'><tr id='E4oBq'><dt id='E4oBq'><q id='E4oBq'><span id='E4oBq'><b id='E4oBq'><form id='E4oBq'><ins id='E4oBq'></ins><ul id='E4oBq'></ul><sub id='E4oBq'></sub></form><legend id='E4oBq'></legend><bdo id='E4oBq'><pre id='E4oBq'><center id='E4oBq'></center></pre></bdo></b><th id='E4oBq'></th></span></q></dt></tr></i><div id='E4oBq'><tfoot id='E4oBq'></tfoot><dl id='E4oBq'><fieldset id='E4oBq'></fieldset></dl></div>
            <bdo id='E4oBq'></bdo><ul id='E4oBq'></ul>

              <small id='E4oBq'></small><noframes id='E4oBq'>

            • <tfoot id='E4oBq'></tfoot><legend id='E4oBq'><style id='E4oBq'><dir id='E4oBq'><q id='E4oBq'></q></dir></style></legend>

                  本文介绍了如何测试预处理器符号是否已#define'd 但没有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 C++ 预处理器指令,是否可以测试预处理器符号是否已定义但没有值?类似的东西:

                  Using C++ preprocessor directives, is it possible to test if a preprocessor symbol has been defined but has no value? Something like that:

                  #define MYVARIABLE
                  #if !defined(MYVARIABLE) || #MYVARIABLE == ""
                  ... blablabla ...
                  #endif
                  

                  我这样做的原因是因为我正在处理的项目应该通过 /DMYSTR=$(MYENVSTR)/DMYSTR=$(MYENVSTR),并且此字符串可能为空.如果用户忘记定义这个字符串,我想确保项目无法编译.

                  The reason why I am doing it is because the project I'm working on is supposed to take a string from the environment through /DMYSTR=$(MYENVSTR), and this string might be empty. I want to make sure that the project fails to compile if user forgot to define this string.

                  推荐答案

                  Soma 宏魔法:

                  #define DO_EXPAND(VAL)  VAL ## 1
                  #define EXPAND(VAL)     DO_EXPAND(VAL)
                  
                  #if !defined(MYVARIABLE) || (EXPAND(MYVARIABLE) == 1)
                  
                  Only here if MYVARIABLE is not defined
                  OR MYVARIABLE is the empty string
                  
                  #endif
                  

                  请注意,如果您在命令行中定义了 MYVARIABLE,则默认值为 1:

                  Note if you define MYVARIABLE on the command line the default value is 1:

                  g++ -DMYVARIABLE <file>
                  

                  这里 MYVARIABLE 的值是空字符串:

                  Here the value of MYVARIABLE is the empty string:

                  g++ -DMYVARIABLE= <file>
                  

                  引用问题解决了:

                  #define DO_QUOTE(X)        #X
                  #define QUOTE(X)           DO_QUOTE(X)
                  
                  #define MY_QUOTED_VAR      QUOTE(MYVARIABLE)
                  
                  std::string x = MY_QUOTED_VAR;
                  std::string p = QUOTE(MYVARIABLE);
                  

                  这篇关于如何测试预处理器符号是否已#define'd 但没有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么宏需要双层间接? 下一篇:如何将 char* 转换为 LPCWSTR?

                  相关文章

                • <i id='4fIb2'><tr id='4fIb2'><dt id='4fIb2'><q id='4fIb2'><span id='4fIb2'><b id='4fIb2'><form id='4fIb2'><ins id='4fIb2'></ins><ul id='4fIb2'></ul><sub id='4fIb2'></sub></form><legend id='4fIb2'></legend><bdo id='4fIb2'><pre id='4fIb2'><center id='4fIb2'></center></pre></bdo></b><th id='4fIb2'></th></span></q></dt></tr></i><div id='4fIb2'><tfoot id='4fIb2'></tfoot><dl id='4fIb2'><fieldset id='4fIb2'></fieldset></dl></div>

                    <small id='4fIb2'></small><noframes id='4fIb2'>

                      <legend id='4fIb2'><style id='4fIb2'><dir id='4fIb2'><q id='4fIb2'></q></dir></style></legend>
                      <tfoot id='4fIb2'></tfoot>
                      • <bdo id='4fIb2'></bdo><ul id='4fIb2'></ul>