Warning C6326

Potential comparison of a constant with another constant

Remarks

This warning indicates a potential comparison of a constant with another constant, which is redundant code. You must check to make sure that your intent is properly captured in the code. In some cases, you can simplify the test condition to achieve the same result.

Code analysis name: CONST_CONST_COMP

Example

The following code generates this warning because two constants are compared:

#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...
  }
}

The following code shows one way to correct this warning by using C++17 if constexpr.

#define LEVEL
const int STD_LEVEL = 5;

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

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

The following code shows one way to correct this warning by using the #ifdef statements to determine which code should execute if C++17 is unavailable:

#define LEVEL
const int STD_LEVEL = 5;

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

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