• <small id='9MiIJ'></small><noframes id='9MiIJ'>

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

          <bdo id='9MiIJ'></bdo><ul id='9MiIJ'></ul>
      2. 如何测试预处理器符号是否已#define'd 但没有值

        时间:2023-10-06
          <tbody id='Ocjdk'></tbody>

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

                <legend id='Ocjdk'><style id='Ocjdk'><dir id='Ocjdk'><q id='Ocjdk'></q></dir></style></legend>
              1. <tfoot id='Ocjdk'></tfoot>
                1. <small id='Ocjdk'></small><noframes id='Ocjdk'>

                  本文介绍了如何测试预处理器符号是否已#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 但没有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                  相关文章

                  最新文章

                  <legend id='q4UMN'><style id='q4UMN'><dir id='q4UMN'><q id='q4UMN'></q></dir></style></legend>
                    <bdo id='q4UMN'></bdo><ul id='q4UMN'></ul>

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

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