mercredi 3 février 2016

how do I assert from constexpr function with exceptions disabled

If I want to assert a detected error from a constexpr function turning off interrupts takes away the suggested method of handling errors (see eric nieblers answer here)

Here is a short code example:

constexpr bool isANumber(char c)
{
    return (c >= '0' && c <= '9');
}

constexpr int charToInt(char c)
{
    return (!isANumber(c))? throw std::logic_error("not a number"):
        c - '0';
}

To the best of my knowledge:

  • static_assert is not allowed because the inputs could be run time values
  • assert may force the function to be run time evaluated
  • throw will not work because I turned off exceptions

What is the work around?

Note: Using C++ in super resource constrained embedded environments one must turn off exceptions because they use excess RAM (my chip has only 16K RAM for example). It is common practice.

Aucun commentaire:

Enregistrer un commentaire