C++ 问题推导模板参数

标签 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> 的包装器。

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。 我觉得我犯了一些明显的错误,但对我来说一切看起来都是正确的。我是否忘记了有关模板的重要信息?

最佳答案

当编译器解析行时,设身处地为自己着想:

return m_ComponentStorage.GetComponent<Component>(index);

m_ComponentStorage的类型依赖于Components...,解析代码时是未知的。您怎么可能知道 GetComponent 应该是什么?

可以是模板成员函数,也可以是简单的成员变量。为了理智起见,指示编译器假设他们正在处理该场景中的成员变量。

你可以告诉编译器它正在处理什么

    return m_ComponentStorage.template GetComponent<Component>(index);

关于C++ 问题推导模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44962507/

相关文章:

c++ - 我的代码看起来不错。为什么我得到 "error: expected primary-expression before ' )' token"?

javascript - 将 C++ 后端连接到 Javascript

c++ - 使用模板时,如何解决以下编译器错误,以实现程序所需的行为?

c++ - 在容器上的循环中同时访问更多元素的 STL 方法

c++ - 为什么 RUMTIME_CLASS 在 VS2008 Debug 版本中没有 "DECLARE_DYNAMIC"通过编译?

c++ - 在 C++ 中计算巨大的斐波那契数模 M

C++ 模板类型定义

c++ - 基类中的 typedef 是否在没有完全限定的情况下在继承类中可见?

c++ - 模板化函数指针作为模板参数

C++ new 和 delete 重载