For these kind of things, recursion is typically a simple solution:
#include <type_traits>
enum ATYPE { Undefined = 0, typeA, typeB, typeC, ATYPE_END };
void reset_Object_counter(std::integral_constant<ATYPE, ATYPE_END>)
{}
template < ATYPE n = Undefined >
void reset_Object_counter(std::integral_constant<ATYPE, n> p = {})
{
Object<p>::counter.store(0);
reset_Object_counter(std::integral_constant<ATYPE,
static_cast<ATYPE>(n+1)>{});
}
For this case, AFAIK, function template specialization works as well (instead of the first overload):
template<>
void reset_Object_counter<ENUM_END>(std::integral_constant<ATYPE, ENUM_END>)
{}
Either way, the usage is just reset_Object_counter();
to set all Object<...>
's counters to 0
.
The integral_constant
solution is actually a bit overkill here, for this problem a non-type template parameter is sufficient (because the function template specialization can take the place of the overload ending the recursion).
template < ATYPE n = Undefined >
void reset_Object_counter()
{
Object<n>::counter.store(0);
reset_Object_counter<static_cast<ATYPE>(n+1)>();
}
template<>
void reset_Object_counter<ENUM_END>()
{}