简体   繁体   中英

How to Iterate over number of variadic template Types

I'am currently learning C++ and i am currently building a very simple Entity Component System. For that i have a Function getComponentType which maps each Component to a uint8_t. A Signature is just a std::bitset

I would like a method like this.

Signature signature = createSignature<TransformComponent, GraphicsComp>();

Lets say TransformComponent gets mapped to 0 and GraphicsComp get mapped to 1. The Signature should now be a std::bitset {1100000...} I know how to do that with non various template methods, the Question is now how would i archive the same with various template types or is there a better solution to do the same.

    template <typename T> Signature createSignature(){
        return Signature(((unsigned long long int)1)<<getComponentType<T>());
    }

    template <typename T, typename R> Signature createSignature(){
        return Signature(
                ((unsigned long long int)1)<<getComponentType<T>() |
                ((unsigned long long int)1)<<getComponentType<R>()
                );
    }

    template <typename T, typename R, typename S> Signature createSignature(){
        return Signature(
                ((unsigned long long int)1)<<getComponentType<T>() |
                ((unsigned long long int)1)<<getComponentType<R>() |
                ((unsigned long long int)1)<<getComponentType<S>()
                );
    }

    template <typename T, typename R, typename S, typename U> Signature createSignature(){
        return Signature(
                ((unsigned long long int)1)<<getComponentType<T>() |
                ((unsigned long long int)1)<<getComponentType<R>() |
                ((unsigned long long int)1)<<getComponentType<S>() |
                ((unsigned long long int)1)<<getComponentType<U>()
                );
    }

From C++ 17 onward you could use a fold expression:

template<typename... T>
return Signature((((unsigned long long int)1) << getComponentType<T>() | ...)); 

The unsigned long long int cast seems a bit weird, but I left it the same as the question to clarify the use of the fold expression:

(statement | ...)

I believe variadic templates are the way to go here. Something along the lines of

template <typename T, typename ...Args>
Signature createSignature(T t, Args...)
{
    return Signature(((unsigned long long int)1)<<getComponentType<T>()) | createSignature(Args...);
}

template<typename T>
Signature createSignature(T t)
{
    return Signature(((unsigned long long int)1)<<getComponentType<T>());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM