In firmware systems, callbacks often need to maintain their own internal state (for example, counting error pulses, debouncing signals, or tracking retry attempts) without relying on global variables. Using static variables inside functions makes such logic hard to reset, reuse, or test. A stateful lambda provides a clean and encapsulated solution.
Your task is to implement a class Ticker that stores a std::function<void()> callback. In main, you must register a lambda expression with this Ticker that:
0mutable so it can modify its internal state⚠️ Language Requirement:
This problem requires C++14 or later, due to the use of lambda capture initialization ([count = 0]).
Program Flow:
Ticker classNN times:cmdcmd is "TICK", invoke the callback using ticker.tick()Input Format:
N (1 ≤ N ≤ 20)N lines: A string cmd ("TICK" or any other string)Input is provided via standard input (stdin).
Output Format:
For every "TICK" command, print:
Count: <new_value> "TICK" commands, produce no outputExample:
Input:
3
TICK
WAIT
TICK
Output:
Count: 1
Count: 2 Constraints:
Input
3 TICK WAIT TICK
Expected Output
Count: 1 Count: 2