如何将 std::any 用作 mapped_type

How to Use std::any as mapped_type

我正在尝试解决 asked yesterday on SO based on

我修改了给定 的代码以使用 std::any 而不是 void*。我目前拥有的代码如下:

#include <iostream>
#include <map>
#include <vector>
#include <any>
#include <typeindex>
struct cStreet{};
struct cHouse{};
struct cComputer{};
struct cBook{};
class cPlayer
{
public:

    struct Properties
    {
        std::vector<cStreet*>    Streets;
        std::vector<cHouse*>     Houses;
        std::vector<cComputer*>  Computers;
        std::vector<cBook*>      Book;
    };
    
    cPlayer(std::string name) : m_name{name}{};
    ~cPlayer(){};
    std::string         m_name{};
    Properties          m_Properties;
    
    
    std::map<std::type_index, std::any> myMap{{typeid(cStreet*), m_Properties.Streets}, {typeid(cHouse*), m_Properties.Houses}, {typeid(cComputer*), m_Properties.Computers}, {typeid(cBook*), m_Properties.Book}};
    
    
    template<typename T> void buy(T& Arg);
    
};
template<typename T> void cPlayer::buy(T& Arg)
{
    std::cout << m_name.c_str() << " : Do you want buy this ?" <<typeid(Arg).name() << std::endl;
    //Todo: Decision (here yes)
    std::any_cast<std::vector<decltype(&Arg)>>(myMap.at(typeid(&Arg))).push_back(&Arg); //THIS DOESN'T ADD ELEMENTS INTO THE VECTORS BECAUSE STD::ANY HAS A COPY OF THE ORIGINAL VECTORS
    
    
}

int main()
{
    //create objects 
    cStreet S;
    cHouse H;
    cComputer C;
    cBook B;
    cPlayer c("anoop");
    
    //lets test our code
    c.buy(S);   
    c.buy(H);
    c.buy(C);
    c.buy(B);
    
}

问题是我写的

std::any_cast<std::vector<decltype(&Arg)>>(myMap.at(typeid(&Arg))).push_back(&Arg);

这不会将(push_back)元素添加到原始向量中,而是将其复制。

如何将元素添加到原始向量 m_Properties.Streetsm_Properties.Houses 等中? 我尝试使用 std::ref 但我无法使用 std::ref.

成功完成

基于,将你的myMap重新定义为:

std::map<std::type_index, std::any> myMap{
  {typeid(cStreet*), std::ref(m_Properties.Streets)}, 
  {typeid(cHouse*), std::ref(m_Properties.Houses)}, 
  {typeid(cComputer*), std::ref(m_Properties.Computers)}, 
  {typeid(cBook*), std::ref(m_Properties.Book)}
};

然后根据Tany转换为对应的reference_wrapper类型,就可以访问到原来的vector:

template<typename T> 
void cPlayer::buy(T& Arg) {
  // ...
  using mapped_type = std::reference_wrapper<std::vector<T*>>; 
  std::any_cast<mapped_type>(myMap.at(typeid(T*))).get().push_back(&Arg);
  // ...
}