如何使用值作为模板函数指针创建映射

时间:2020-04-03 19:27:29

标签: c++ c++11 maps function-pointers shared-ptr

以下代码无法编译,请在缺少的地方帮助我。我有不同返回类型的成员函数,我只需要传递一个字符串名就可以调用这些函数。因此,我创建了一个地图。以下是符合我的要求的示例,但是我无法编译。

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",&GetBasicClassInfo<BasicClass>()->GetValue}
};

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

0 个答案:

没有答案