|
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/5/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 | int main() |
23 | 21 | { |
24 | 22 | int a = 0, b = 0; |
25 | | - if ( a = b ) // C4706 |
| 23 | + if (a = b) // C4706 |
26 | 24 | { |
27 | 25 | } |
28 | 26 | } |
29 | 27 | ``` |
30 | 28 |
|
31 | | -The warning will occur even if you double the parentheses around the test condition: |
| 29 | +Suppress the warning with `((`*expression*`))`. For example: |
32 | 30 |
|
33 | 31 | ```cpp |
34 | | -// C4706b.cpp |
35 | 32 | // compile with: /W4 |
36 | 33 | int main() |
37 | 34 | { |
38 | 35 | int a = 0, b = 0; |
39 | | - if ( ( a = b ) ) // C4706 |
| 36 | + if ((a = b)) // No warning |
40 | 37 | { |
41 | 38 | } |
42 | 39 | } |
43 | 40 | ``` |
44 | 41 |
|
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: |
| 42 | +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: |
46 | 43 |
|
47 | 44 | ```cpp |
48 | | -// C4706c.cpp |
49 | 45 | // compile with: /W4 |
50 | 46 | int main() |
51 | 47 | { |
52 | 48 | int a = 0, b = 0; |
53 | | - if ( a == b ) |
| 49 | + if (a == b) |
54 | 50 | { |
55 | 51 | } |
56 | 52 | } |
57 | 53 | ``` |
58 | 54 |
|
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: |
| 55 | +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: |
60 | 56 |
|
61 | 57 | ```cpp |
62 | | -// C4706d.cpp |
63 | 58 | // compile with: /W4 |
64 | 59 | int main() |
65 | 60 | { |
66 | 61 | int a = 0, b = 0; |
67 | | - if ( ( a = b ) != 0 ) |
| 62 | + if ((a = b) != 0) |
68 | 63 | { |
69 | 64 | } |
70 | 65 | } |
|
0 commit comments