Here is a simple example sketch compiled under C++20 for an ESP32_C3 (using Arduino IDE 2.3.3). It yields a message that this use of 'volatile' is deprecated.
This design pattern is quite commonly used in Arduino sketches. What is the simplest recommended way of structuring this to avoid such messages?
C:\Users\6v6gt\AppData\Local\Temp\.arduinoIDE-unsaved2025019-12368-1726qco.z5dh\sketch_jan19b\sketch_jan19b.ino: In function 'void toggle()':
C:\Users\6v6gt\AppData\Local\Temp\.arduinoIDE-unsaved2025019-12368-1726qco.z5dh\sketch_jan19b\sketch_jan19b.ino:14:14: warning: compound assignment with 'volatile'-qualified left operand is deprecated [-Wvolatile]
14 | pulses += 1;
| ~~~~~~~^~~~
pulses = pulses + 1 ; // does indeed work pulses++ ; // does not and gives the following message: . . .
C:\Users\6v6gt\AppData\Local\Temp\.arduinoIDE-unsaved2025019-12368-1726qco.z5dh\sketch_jan19b\sketch_jan19b.ino: In function 'void toggle()':
C:\Users\6v6gt\AppData\Local\Temp\.arduinoIDE-unsaved2025019-12368-1726qco.z5dh\sketch_jan19b\sketch_jan19b.ino:18:3: warning: '++' expression of 'volatile'-qualified type is deprecated [-Wvolatile]
18 | pulses++;
| ^~~~~~
That is an interesting article.
So it looks like the suggestion solution is this:
I guess the "pulses = pulses + 1" variant is OK because the user is considered less likely to believe that it is an atomic operation because one operand appears twice.