dimanche 4 juin 2017

Template being compiled as empty

I have a template class, who will be storing all types passed in into an std::tuple. I have a set of helper functions, one set will determine the index of the type in the std::tuple, the other will generate a bitset from that index.

For example, if I have some std::tuple like:

using typelist = std::tuple<int, char, double, float>;

Then, I call my helper function to determine the indicies of the types:

IndexOf<int, typelist>::value; //evaluates to 0
IndexOf<double, typelist>::value; //evaluates to 2

And the subsequent bitsets of the two would be:

GenerateBitset<int>(); //Returns [0 0 0 1]
GenerateBitset<double>(); //Returns [0 1 0 0]
GenerateBitset<int, double>(); //Returns [0 1 0 1]

However, I am getting the following compile-time error: Use of undefined type 'aecs::mpl::IndexOf<T, std::tuple<>>. This only happens when I start calling IndexOf from my template class. On its own, it compiles and behaves fine. Here is the code for my template class (World), which contains the function GenerateBitset, which in turns uses the metafuncton IndexOf.

namespace aecs
{
  template<typename... Components>
  class World
  {
    using ComponentList = std::tuple<Components...>;
    using ComponentMask = std::bitset<sizeof...(Components)>;

    //Entity Metadata
    //Used for storing the component mask of an entity
    struct Metadata
    {
      Entity m_Entity;
      ComponentMask m_ComponentMask;
    };

  public:
    World() {}
    ~World() {}


    template<typename T>
    ComponentMask ShowComponentMask()
    {
      auto mask = GenerateBitset<T>();
      return mask;
    }
    template<typename T, typename T2, typename... Other>
    ComponentMask ShowComponentMask()
    {
      auto mask = ShowComponentMask<T>() | ShowComponentMask<T2, Other...>();
      return mask;
    }



    //Private functions
  private:


    //Calculates a ComponentMask, given the types passed into it.
    template<typename T>
    ComponentMask GenerateBitset()
    {
      ComponentMask mask;
      mask.set(aecs::mpl::IndexOf<T, ComponentList>::value);
      return mask;
    }
    template<typename T, typename T2, typename... Other>
    ComponentMask GenerateBitset()
    {
      return GenerateBitset<T>() | GenerateBitset<T2, Other...>();
    }


    //Private data
  private:
    std::vector<Metadata> m_EntityMetadata;
    std::vector<Entity> m_Entities;
  };
}

IndexOf:

namespace aecs
{
  namespace mpl
  {
    template<typename T, class Tuple>
    struct IndexOf;

    template<typename T, class... Types>
    struct IndexOf < T, std::tuple<T, Types...>>
    {
      static constexpr std::size_t value = 0;
    };

    template<typename T, typename U, class... Types>
    struct IndexOf<T, std::tuple<U, Types...>>
    {
      static constexpr std::size_t value = 1 + IndexOf<T, std::tuple<Types...>>::value;
    };
  }
}

Aucun commentaire:

Enregistrer un commentaire