Skip to content

Commit

Permalink
fix: Protection against v == 0 in template_switch (#1707)
Browse files Browse the repository at this point in the history
Add a check in `template_switch` methods in case `v == 0` (i.e. no measurements) and add corresponding error message
  • Loading branch information
CarloVarni committed Dec 9, 2022
1 parent 6045480 commit ce28f69
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Core/include/Acts/Utilities/Helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ auto template_switch(size_t v, Args&&... args) {
if (v == N) {
return Callable<N>::invoke(std::forward<Args>(args)...);
}
if (v == 0) {
std::cerr << "template_switch<Fn, " << N << ", " << NMAX << ">(v=" << v
<< ") is not valid (v == 0 and N != 0)" << std::endl;
std::abort();
}
if constexpr (N < NMAX) {
return template_switch<Callable, N + 1, NMAX>(v,
std::forward<Args>(args)...);
Expand All @@ -417,6 +422,11 @@ auto template_switch_lambda(size_t v, Lambda&& func, Args&&... args) {
return func(std::integral_constant<size_t, N>{},
std::forward<Args>(args)...);
}
if (v == 0) {
std::cerr << "template_switch<Fn, " << N << ", " << NMAX << ">(v=" << v
<< ") is not valid (v == 0 and N != 0)" << std::endl;
std::abort();
}
if constexpr (N < NMAX) {
return template_switch_lambda<N + 1, NMAX>(v, func,
std::forward<Args>(args)...);
Expand Down

0 comments on commit ce28f69

Please sign in to comment.