C6326C6326
предупреждение C6326: возможное сравнение константы с другой константойwarning C6326: potential comparison of a constant with another constant
Это предупреждение указывает на возможное сравнение константы с другой константой, которая является избыточным кодом.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.
Пример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...
}
}
В следующем коде показан один из способов устранения этого предупреждения с помощью C++ 17 if constexpr
.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...
}
В следующем коде показан один из способов устранения этого предупреждения с помощью инструкций #ifdef, чтобы определить, какой код должен выполняться, если C++ 17 недоступен: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
}