Slide 47
Slide 47 text
Enter P1002: Try-catch blocks in constexpr functions
template
constexpr std::array
square(std::array array, int from, int to)
{
try {
for (; from != to; ++from)
{
array.at(from) = array.at(from) * array.at(from);
}
} catch (std::out_of_range const& e) {
// ...
}
return array;
}
// Works because we never execute a throw statement
constexpr auto OK = square(std::array{1, 2, 3, 4}, 0, 4);
// Fails
constexpr auto NOT_OK = square(std::array{1, 2, 3, 4}, 0, 10);