I often find myself writing certain types of code in embedded processing, and I think it would be great if C++ had some simpler syntax options. For instance, instead of having to implement a manual counter to execute code every 50 passes through a loop, it would be nice to use a hypothetical `every(50) {...}` syntax. Additionally, for executing code only when a value changes, a cleaner approach would be using something like `changed(val) {...}` rather than the current method of checking against a stored value. What are your thoughts on these kinds of suggestions?
5 Answers
A different approach could be to use a loop like this:
```
while(true)
{
for(int i=0; i<50; i++)
{
// Regular task here
}
// Once every 50 loops, execute this task
}
```
Really, you're just asking for a simpler abstraction. C++ is good as a straightforward language, and sometimes you can handle these tasks better with interrupts instead.
I think the concept of adding an `every()` function is a bit too specific to justify its inclusion. Like in my main loop, I might prefer something that executes code every 50 milliseconds instead.
It's important to balance features and maintainability. Every new feature makes software heavier and can lead to readability issues. Your suggestions are quite specific, and while they could simplify certain tasks, they might also complicate the language overall. If it's just to save a few lines of code, that could be achieved with well-commented macros or snippets.
You could use `if (i % 50 == 0)` to efficiently run code every 50 iterations, but it's worth noting that this won't reset a counter to zero. Plus, SystemVerilog has a feature like this: `if (i > 0 && $changed(value))`.
Have you tried creating a `static inline` function for what you're suggesting? With optimizations enabled, the compiler often removes function calls completely, keeping your code efficient without any drawbacks.

Just a heads up, using `i % 50` can lead to bugs if the value overflows, so be cautious with that.