• <tfoot id='hTQyh'></tfoot>

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

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

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

        我的代码如何区分编译时常量和变量?

        时间:2023-10-06

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

            <tbody id='Ks0gw'></tbody>
        • <tfoot id='Ks0gw'></tfoot><legend id='Ks0gw'><style id='Ks0gw'><dir id='Ks0gw'><q id='Ks0gw'></q></dir></style></legend>
              • <small id='Ks0gw'></small><noframes id='Ks0gw'>

                1. 本文介绍了我的代码如何区分编译时常量和变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这是我的问题.我有一个 BINARY_FLAG 宏:

                  Here's my problem. I have a BINARY_FLAG macro:

                  #define BINARY_FLAG( n ) ( static_cast<DWORD>( 1 << ( n ) ) )
                  

                  可以像这样使用(常量"场景):

                  Which can be used either like this ("constant" scenario):

                  static const SomeConstant = BINARY_FLAG( 5 );
                  

                  或者像这样(可变"场景):

                  or like this ("variable" scenario):

                  for( int i = 0; i < 10; i++ ) {
                      DWORD flag = BINARY_FLAG( i );
                      // do something with the value
                  }
                  

                  这个宏根本不是万无一失的——你可以通过 -134 那里最多会有一个警告,但行为将是未定义的.我想让它更万无一失.

                  This macro is not foolproof at all - one can pass -1 or 34 there and there will at most be a warning yet behavior will be undefined. I'd like to make it more foolproof.

                  对于常量场景,我可以使用模板:

                  For the constant scenario I could use a template:

                  template<int Shift> class BinaryFlag {
                  staticAssert( 0 <= Shift && Shift < sizeof( DWORD) * CHAR_BIT );
                  public:
                  static const DWORD FlagValue = static_cast<DWORD>( 1 << Shift );
                  };
                  #define BINARY_FLAG( n ) CBinaryFlag<n>::FlagValue
                  

                  但这不适用于可变"场景——我需要一个运行时断言:

                  but this will not go for the "variable" scenario - I'd need a runtime assertion there:

                  inline DWORD ProduceBinaryFlag( int shift )
                  {
                      assert( 0 <= shift && shift < sizeof( DWORD) * CHAR_BIT );
                      return static_cast<DWORD>( 1 << shift );
                  }
                  #define BINARY_FLAG( n ) ProduceBinaryFlag(n)
                  

                  后者很好,但没有编译时检查.当然,我希望在可能的情况下进行编译时检查,否则进行运行时检查.在任何时候,我都希望尽可能少的运行时开销,因此我不希望在可以进行编译时检查时进行函数调用(可能不会被内联).

                  The latter is good, but has no compile-time checks. Of course, I'd like a compile-time check where possible and a runtime check otherwise. At all times I want as little runtime overhead as possible so I don't want a function call (that maybe won't be inlined) when a compile-time check is possible.

                  我看到了这个问题,但看起来不是同一个问题.

                  I saw this question, but it doesn't look like it is about the same problem.

                  是否有一些结构可以根据作为标志号传递的表达式是编译时常量还是变量来允许在两者之间交替?

                  Is there some construct that would allow to alternate between the two depending on whether the expression passed as a flag number is a compile-time constant or a variable?

                  推荐答案

                  不可能将参数传递给宏或函数并确定它是编译时常量还是变量.

                  It's not possible to pass an argument to a macro or function and determine if it's compile time constant or a variable.

                  最好的方法是您#define BINARY_FLAG(n) 使用编译时代码并将该宏放置在任何地方,然后编译它.您将在 n 将成为运行时的地方收到编译器错误.现在,您可以使用运行时宏 BINARY_FLAG_RUNTIME(n) 替换这些宏.这是唯一可行的方法.

                  The best way is that you #define BINARY_FLAG(n) with compile time code and place that macro everywhere and then compile it. You will receive compiler-errors at the places where n is going to be runtime. Now, you can replace those macros with your runtime macro BINARY_FLAG_RUNTIME(n). This is the only feasible way.

                  这篇关于我的代码如何区分编译时常量和变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:带有 C++11 的 Visual Studio 2008 下一篇:如何在我的 C++ 源代码中查找(和替换)所有旧的

                  相关文章

                  最新文章

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

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