samedi 30 avril 2016

C++ Template Variadic - Call a member function once for every template argument

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();
}

Aucun commentaire:

Enregistrer un commentaire