1
我將std::vector
的集合存儲在std::tuple
中。但是,當我從元組中獲取元素並對其進行修改時,我只修改返回的元素的副本。從std :: tuple獲取副本而不是引用
template<typename... Ts>
class ComponentStore
{
public:
ComponentStore()
{
}
~ComponentStore()
{
}
template<typename T>
std::vector<T>& Get()
{
return std::get<std::vector<T>>(m_components);
}
private:
std::tuple<std::vector<Ts>...> m_components;
};
這是我計劃如何使用ComponentStore
類:使用汽車本身
ecs::component::ComponentStore<ecs::component::Position, ecs::component::Velocity> comstore;
//Get the position vector
auto positionvec = comstore.Get<ecs::component::Position>();
//Add a new position
positionvec.emplace_back(ecs::component::Position{});
//Later on, get the position vector again
auto positionvec2 = comstore.Get<ecs::component::Position>();
//But it's empty??? this is wrong. It should have 1 element.