You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-warnings/compiler-warning-level-4-c4706.md
+6-24Lines changed: 6 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,21 @@
1
1
---
2
2
description: "Learn more about: Compiler Warning (level 4) C4706"
3
3
title: "Compiler Warning (level 4) C4706"
4
-
ms.date: "11/04/2016"
4
+
ms.date: "3/4/2025"
5
5
f1_keywords: ["C4706"]
6
6
helpviewer_keywords: ["C4706"]
7
-
ms.assetid: 89cd3f4f-812c-4a4b-9426-65a5a6d1b99c
8
7
---
9
8
# Compiler Warning (level 4) C4706
10
9
11
-
assignment within conditional expression
10
+
> assignment used as a condition
12
11
13
-
The test value in a conditional expression was the result of an assignment.
12
+
The test value in a conditional expression is the result of an assignment.
14
13
15
14
An assignment has a value (the value on the left side of the assignment) that can be used legally in another expression, including a test expression.
16
15
17
16
The following sample generates C4706:
18
17
19
18
```cpp
20
-
// C4706a.cpp
21
19
// compile with: /W4
22
20
intmain()
23
21
{
@@ -28,24 +26,9 @@ int main()
28
26
}
29
27
```
30
28
31
-
The warning will occur even if you double the parentheses around the test condition:
29
+
If your intention is to test a relation, not to make an assignment, use the `==` operator. For example, the following tests whether a and b are equal:
32
30
33
31
```cpp
34
-
// C4706b.cpp
35
-
// compile with: /W4
36
-
intmain()
37
-
{
38
-
int a = 0, b = 0;
39
-
if ( ( a = b ) ) // C4706
40
-
{
41
-
}
42
-
}
43
-
```
44
-
45
-
If your intention is to test a relation and not to make an assignment, use the `==` operator. For example, the following line tests whether a and b are equal:
46
-
47
-
```cpp
48
-
// C4706c.cpp
49
32
// compile with: /W4
50
33
intmain()
51
34
{
@@ -56,15 +39,14 @@ int main()
56
39
}
57
40
```
58
41
59
-
If you intend to make your test value the result of an assignment, test to ensure that the assignment is non-zero or not null. For example, the following code will not generate this warning:
42
+
If you intend to make your test value the result of an assignment, test to ensure that the assignment is non-zero or non-null. For example, the following code doesn't generate this warning:
0 commit comments