lundi 7 décembre 2015

Associating an array with a variadic template

I'm now learning a little about templates and templates in C++11, C++14 and C++1z. I'm trying to write a variadic class template with an inside class that will associate an int to every template argument - and have a constexpr method that returns its array representation.

Let's say that I have ensured that the template cannot receive two of the same type as an argument. I was thinking about doing it somewhat like this:

template <typename... Types>
struct MyVariadicTemplate {
    //we know that all types in Types... are different
    template <int... Values> 
    struct MyInnerTemplate {
        //I need to make sure that sizeof...(Values) == sizeof...(Types)
        constexpr std::array<int, sizeof...(Values)> to_array() {
            std::array<int, sizeof...(Values)> result = {Values...};
            return result;
            // this is only valid since C++14, as far as I know
        }
    };
};

this code should be valid (if it's not, I'd love to know why). Now, I'd like to add another inner template:

template <typedef Type>
struct AnotherInnerTemplate {};

that has a public typedef, which represents MyInnerTemplate with one on the position of Type in Types... and zeros elsewhere - and here I'm lost. I don't know how to proceed

I would appreciate any hint on how that can be done - and if I'm heading towards the wrong direction, I hope somebody can give me a hint on how to do that.

Aucun commentaire:

Enregistrer un commentaire