vendredi 3 avril 2020

how to create a map with value as templatized function pointer

Below code doesn't compile, please help me where I'm missing. I have member functions with different return types and I need to call those functions by just passing a string name. So I have created a map, Below is the example which matches my requirement, but I'm not able to compile.

template<typename T1>
shared_ptr<T1> GetBasicClassInfo()
{
    T1 obj;
    shared_ptr<T1> pobj( new T1);
    return pobj;
}

class BasicClass
{
  public:
     std::string GetValue()
     {
         return "Successfull";
     }
     int GetInt()
     {
         return 5;
     }
};

template<typename T>
std::map<std::string, std::function<T()>> GetPropertyValue ={
   {"int",&GetBasicClassInfo<BasicClass>()->GetInt},
   {"string",&GetAssetInfoObject<BasicClass>()->GetValue}
};

int main()
{
   cout<<GetPropertyValue["int"]<<endl;
   cout<<GetPropertyValue["string"];
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire