I am trying to solve the problem asked yesterday on SO based on this answer.
I have modified the code given here to use std::any
instead of void*
. The code that i currently have is as follows:
#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);
}
The problem is that when i wrote
std::any_cast<std::vector<decltype(&Arg)>>(myMap.at(typeid(&Arg))).push_back(&Arg);
this does not adds(push_back
) element into the original vectors but a copy of it.
How can i add element into the original vectors m_Properties.Streets
, m_Properties.Houses
etc? I tried using std::ref
but i was not able to successfully do it using std::ref
.
CodePudding user response:
Based on this answer, redefine your myMap
as:
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)}
};
Then cast any
to the corresponding reference_wrapper
type according to T
to access the original 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);
// ...
}