我的代码如何告诉编译时常数与变量? [英] How could my code tell a compile-time constant versus a variable?

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

问题描述

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

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

这个(常量场景):

  static const SomeConstant = BINARY_FLAG 

或类似这样(可变情景):


$ b b

  for(int i = 0; i <10; i ++){
DWORD flag = BINARY_FLAG(i);
//使用值
执行操作

此宏不是万无一失可以通过 -1 34 ,最多会有一个警告,但行为将被定义。



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

  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

去变量场景 - 我需要一个运行时断言:

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

是好的,但没有编译时检查。当然,我想要一个编译时检查可能和运行时检查否则。在任何时候,我想尽可能少的运行时开销,所以我不想要一个函数调用(也许不会内联),当编译时检查是可能的。



我看到这个问题,但它看起来不像是关于同样的问题。



有没有一些构造,允许在两者之间交替,取决于作为标志号传递的表达式是编译时常量还是变量?

解决方案

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

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


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
}

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.

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.

这篇关于我的代码如何告诉编译时常数与变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆