Reputation: 2355
I have an EntityComponent-System and there's one component which should call functions on various other components. For this example here I've chosen a simple combination of the results of a boolean function.
The ResultCombinerComponent has variadic template arguments and for each of these arguments it should call a function, in the order that the template arguments are given. The order is very important.
Afterwards the results of the function calls are combined.
In the code below I just replaced the parts that I don't know how to achieve with psuedo-C++
template<typename... Args>
class ResultCombinerComponent
{
template<typename T>
bool calcSingleResult()
{
return getComponent<T>()->calcResult();
}
bool calcResult()
{
bool result = true;
for (int i = 0; i < sizeof...(Args); i++) // doesn't work
{
if (!calcSingleResult<Args(i)>() // doesn't work
{
result = false;
break;
}
}
return result;
}
}
class A
{
bool calcResult();
}
class B
{
bool calcResult();
}
class C
{
bool calcResult();
}
Upvotes: 0
Views: 519
Reputation: 217428
in C++17, you may do:
bool calcResult()
{
return (calcSingleResult<Args>() && ...);
}
In c++11, you have to have other methods:
template <typename T>
bool calcImpl()
{
return calcSingleResult<T>();
}
template <typename T, typename T2, typename...Ts>
bool calcImpl()
{
return calcSingleResult<T>() && calcImpl<T2, Ts...>();
}
bool calcResult()
{
return calcImpl<Args...>();
}
Upvotes: 2