A hurdle in using C++ (instead of handling C++ as "C with a few extra features") in low-level embedded development is caused by interrupts. Often there are cases where time must be measured in individual CPU cycles, and some classes have no business being used there. Implicit conversions, copy constructors and similar things can cause issues when used within interrupts, especially if they happen behind the scenes. On the other hand, some carefully designed classes could be meaningfully used in interrupts.
This raises a question: is there an elegant way to signal the compiler that a specific class, function, or variable (or just code lines) should throw a compile error when used inside an interrupt? (to not have to rely so heavily on comments stating //Never call this from an interrupt!
)
(If not, would it make sense to try getting something like this into the next C++ standard? I'm thinking of something like how the constexpr
specifier informs the compiler what a function or variable can or cannot be used for. Would a nointerrupt
specifier make sense, or does something already exist that provides that functionality?)