2016-08-14 66 views
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. 

回答

4

,創建推導出非引用類型的變量,因此

auto positionvec = comstore.Get<ecs::component::Position>(); 

創建一個新的向量;

auto& positionvec = comstore.Get<ecs::component::Position>(); 

您可以通過使用自動&解決這個問題

相關問題