C6326

更新:2007 年 11 月

警告 C6326:可能对两个常数进行比较

此警告意味着可能对两个常数进行比较,这是冗余代码。必须检查代码,确保在其中正确实现您的目的。在某些情况下,可以简化测试条件,同时实现相同的结果。

示例

在下面的代码中,因为对两个常数进行比较,所以会生成此警告:

#define LEVEL  
const int STD_LEVEL = 5;

const int value = 
#ifdef LEVEL
10;
#else 
5;
#endif

void f()
{
  if( value > STD_LEVEL)
  {
    // code...
  }
  else
  {
    // code...
  }
}

下面的代码演示一种更正此警告的方法,该方法使用 #ifdef 语句来确定应当执行的代码:

#define LEVEL  
const int STD_LEVEL = 5;

const int value = 
#ifdef LEVEL
10;
#else 
5;
#endif

void f ()
{
#ifdef LEVEL
  {
    // code...
  }
#else
  {
    // code...
  }
#endif
} 

请参见

参考

编译器警告(等级 1)C4127