﻿# V721\. The VARIANT\_BOOL type is used incorrectly\. The true value \(VARIANT\_TRUE\) is defined as \-1\.

The analyzer has detected an incorrect use of the VARIANT\_BOOL type\. The reason is that the value true \(VARIANT\_TRUE\) is designated as \-1\. Many programmers are unaware of this detail and tend to use this type incorrectly\.

This is how the VARIANT\_TRUE type and constants denoting "true" and "false" are declared:

```cpp
typedef short VARIANT_BOOL;
#define VARIANT_TRUE ((VARIANT_BOOL)-1)
#define VARIANT_FALSE ((VARIANT_BOOL)0)
```

Let's take a look at a few examples when the VARIANT\_TRUE type is used incorrectly\. In all the cases, the programmer expects the condition to be true, while it is actually always false\.

Example 1\.

```cpp
VARIANT_BOOL variantBoolTrue = VARIANT_TRUE;
if (variantBoolTrue == true) //false
```

If we substitute the value into the expression, we'll get \(\(short\)\(\-1\) \=\= true\)\. When this expression is evaluated, 'true' will turn into '1'\. The condition \(\-1 \=\= 1\) is false\.

The correct code:

```cpp
if (variantBoolTrue == VARIANT_TRUE)
```

Example 2\.

```cpp
VARIANT_BOOL variantBoolTrue = TRUE;
if (variantBoolTrue == VARIANT_TRUE) //false
```

The programmer made a mistake here and used TRUE instead of VARIANT\_TRUE\. It will result in the variantBoolTrue variable being assigned the value 1\. This value is illegal for variables of the VARIANT\_BOOL type\.

If we substitute the value into the expression, we will get \(1 \=\= \(short\)\(\-1\)\)\.

The correct code:

```cpp
VARIANT_BOOL variantBoolTrue = VARIANT_TRUE;
```

Example 3\.

```cpp
bool bTrue = true;
if (bTrue == VARIANT_TRUE) //false
```

Let's expand the expression: \(true \=\= \(short\)\(\-1\)\)\. When it is evaluated, 'true' will turn into '1'\. The condition \(1 \=\= \-1\) is false\.

It's not easy to suggest a correct version of this code as it is just fundamentally incorrect\. One can't mix variables of the 'bool' type and values of the 'VARIANT\_TRUE' type\.

There are numbers of other examples like these to be found in code\. For instance, when a function's formal argument is of the VARIANT\_BOOL type but it is the 'true' value that will be passed as the actual one\. Another example is when a function returns an incorrect value\. And so on and so forth\.

**The most important thing you should keep in mind is that you can't mix the VARIANT\_BOOL type with the types BOOL, bool, and BOOLEAN\.**

**References:**

1. MSDN\. [VARIANT\_BOOL](https://msdn.microsoft.com/en-us/library/cc237864.aspx)\.
1. The Old New Thing\. [BOOL vs\. VARIANT\_BOOL vs\. BOOLEAN vs\. bool](https://devblogs.microsoft.com/oldnewthing/2004/12/22)\.