在模板中存储模板类函数指针

时间:2018-08-08 08:24:05

标签: c++ c++11 templates function-pointers

模板类

template <class F>
class MyClass
{
public:

inline static bool isPresent()
{
    if (F::getDetails())
    {
        return F::isPresent();
    }

    return false;
};
};

基类

class Base
{
public:

inline static bool isPresent()
{
    ...
}

static bool getDetails();
};

派生类

class Derived : public Base
{
public:

};

模板类函数调用

const bool isEnabled = MyClass<Derived>::isPresent();

我想将上述函数调用作为指针存储在map中。但是,模板参数可以是不同的派生类。我该怎么做?

using MyClassPtr = bool (MyClass<Base>::*)();
map<string,MyClassPtr>  data;
data = {  
    {"abc", &MyClass<Derived>::isPresent},
    {"wer", &MyClass<Derived1>::isPresent}
};  

我收到以下错误:

error: no match for ‘operator=’ (operand types are ‘std::map<std::basic_string<char>, bool (App::MyClass<App::Derived>::*)()>’ and ‘<brace-enclosed initializer list>’)
 data = {

0 个答案:

没有答案