C ++问题推断模板参数

时间:2017-07-07 04:20:55

标签: c++ templates c++14

我有一个模板类ComponentCache,它本质上是std::vector的包装器,除了它只提供对数据的随机访问。

template<class T>
class ComponentCache
{
public:
  ComponentCache()
  {
    m_Components.reserve(1024);
  }

  ~ComponentCache()
  { }

  //Get a component stored at some index.
  //If the index is greater than the size of the index,
  //resize it to the size of the index + 128
  T& GetComponent(const std::uint32_t& index)
  {
    if(index >= m_Components.size())
    {
      std::uint32_t newSize = index + 128;
      m_Components.resize(newSize);
    }

    return m_Components.at(index);
  }
private:
  std::vector<T> m_Components;
};

我有另一个模板类ComponentStorage,它本质上是std :: tuple&gt;的包装器。

template<class... T>
class ComponentStorage
{
public:
  ComponentStorage()
  { }
  ~ComponentStorage()
  { }


  //Gets a component at some index from the tuple of components
  template<class Component>
  Component& GetComponent(const aecs::Entity& index)
  {
   return std::get<aecs::Component::ComponentCache<Component>>
     (m_ComponentLines).GetComponent(index);
  }

private:
  std::tuple<aecs::Component::ComponentCache<T>...> m_ComponentLines;
};

最后,我有另一个模板类World,它充当了一个保持内存池和ComponentStorage类在逻辑上靠近彼此的位置。

template<class... Components>
class World
{
  struct EntityMetadata
  {
    std::bitset<sizeof...(Components)> ComponentBitset;
    bool IsActive = false;
  };
public:
  World(std::size_t size = 128)
  {
    m_EntityMetadata.resize(size);
  }
  ~World()
  { }

  /*Entity Management*/
  const aecs::Entity& CreateEntity()
  {
    auto entity = m_EntityPool.Create();
    if(entity != 0xFFFFFFFF)
    {
      auto entityIndex = aecs::GetIndexFromEntity(entity);
      m_EntityMetadata.at(entityIndex).IsActive = true;
    }
    return std::move(entity);
  }
  void DestroyEntity(const aecs::Entity& entity)
  {
    if(entity == 0xFFFFFFFF)
    {
      return;
    }
    auto entityIndex = aecs::GetIndexFromEntity(entity);
    m_EntityMetadata.at(entityIndex).IsActive = false;
    m_EntityPool.Destroy(entity);
  }
  const std::vector<aecs::Entity>& GetActiveEntities()
  {
    return m_EntityPool.GetActiveEntities();
  }
  const bool IsEntityActive(const aecs::Entity& entity)
  {
    auto entityIndex = aecs::GetIndexFromEntity(entity);
    return m_EntityMetadata.at(entityIndex).IsActive;
  }

  /*Component Management*/
  template<class Component>
    Component& GetComponentFromEntity(const aecs::Entity &entity)
  {
    const std::uint32_t index = aecs::GetIndexFromEntity(entity);
    return m_ComponentStorage.GetComponent<Component>(index);
  }
  template<class Component>
    void AddComponentToEntity(const aecs::Entity& entity)
  {
    //TODO: Implement

    auto entityIndex = aecs::GetIndexFromEntity(entity);

    m_EntityMetadata.at(entityIndex).ComponentBitset.set(
      aecs::mpl::IndexOf<Component, std::tuple<Components...>>::value);
  }
  template<class Component>
    void RemoveComponentFromEntity(const aecs::Entity& entity)
  {
    //TODO: Implement
    auto componentIndex = aecs::mpl::IndexOf<Component,
      std::tuple<Components...>>::value;
    auto entityIndex = aecs::GetIndexFromEntity(entity);

    m_EntityMetadata.at(entityIndex).ComponentBitset.reset(componentIndex);

  }
  template<class Component>
    const bool EntityHasComponent(const aecs::Entity& entity)
  {
    auto componentIndex = aecs::mpl::IndexOf<Component,
      std::tuple<Components...>>::value;
    auto entityIndex = aecs::GetIndexFromEntity(entity);

    std::bitset<sizeof...(Components)> tempBitset;
    tempBitset.set(componentIndex);


    return (m_EntityMetadata.at(entityIndex).ComponentBitset & tempBitset) ==
                                                           tempBitset;
  }

private:
  aecs::EntityPool m_EntityPool;
  aecs::ComponentStorage<Components...> m_ComponentStorage;

  std::vector<EntityMetadata> m_EntityMetadata;
};

将它们放在一起,它应该是这样的:

struct Position
{
  float x;
  float y;
};

struct Velocity
{
  float x;
  float y;
};

int main() {

  aecs::World<Position, Velocity> w;

  const auto e = w.CreateEntity();
  Velocity& v1 = w.GetComponentFromEntity<Velocity>(e);
  return 0;
}

但是,我收到有关GetComponentFromEntity的以下编译错误:

error: expected primary-expression before '>' token
     return m_ComponentStorage.GetComponent<Component>(index);

如果我将其切换为自动推断出Component的类型,我会收到以下错误:

E:\CLion\AECS\aecs/World/World.h: In instantiation of 'Component& aecs::World<Components>::GetComponentFromEntity(const Entity&) [with Component = Velocity; Components = {Position, Velocity}; aecs::Entity = unsigned int]':
E:\CLion\AECS\main.cpp:26:54:   required from here
E:\CLion\AECS\aecs/World/World.h:69:49: error: no matching function for call to 'aecs::ComponentStorage<Position, Velocity>::GetComponent(const uint32_t&)'
     return m_ComponentStorage.GetComponent(index);
                                                 ^
In file included from E:\CLion\AECS\aecs/World/World.h:9:0,
                 from E:\CLion\AECS\main.cpp:2:
e:\clion\aecs\aecs\componentstorage\componentstorage.h:27:14: note: candidate: template<class Component> Component& aecs::ComponentStorage<T>::GetComponent(const Entity&) [with Component = Component; T = {Position, Velocity}]
   Component& GetComponent(const aecs::Entity& index)
              ^
e:\clion\aecs\aecs\componentstorage\componentstorage.h:27:14: note:   template argument deduction/substitution failed:
In file included from E:\CLion\AECS\main.cpp:2:0:
E:\CLion\AECS\aecs/World/World.h:69:49: note:   couldn't deduce template parameter 'Component'
     return m_ComponentStorage.GetComponent(index);

似乎编译器在编译时推断Component的类型没有问题,因为Component是返回的内容。 我觉得我犯了一些明显的错误,但一切看起来都对我不对。我忘记了关于模板的重要事项吗?

1 个答案:

答案 0 :(得分:1)

在解析该行时,让自己置身于编译器的脚下:

return m_ComponentStorage.GetComponent<Component>(index);

m_ComponentStorage的类型取决于Components...,这在解析代码时是未知的。你怎么能说出GetComponent应该是什么?

它可以是模板成员函数,但它也可以是一个简单的成员变量。出于理智的考虑,编译器被指示假设他们正在处理该场景中的成员变量。

您可以告诉编译器它使用

处理什么
    return m_ComponentStorage.template GetComponent<Component>(index);