【发布时间】:2018-08-08 08:24:05
【问题描述】:
模板类
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 = {
【问题讨论】:
-
isEnabled根本不是一个函数。你到底想要什么?请给minimal reproducible example -
@appleapple 这只是
MyClass中的一些方法 -
@TheQuantumPhysicist 我相信我看到了有问题的
const bool isEnabled。其他地方都没有。 -
我删除了我的答案,因为有人在没有解释原因的情况下投了反对票。如果这里的人不在乎,那么我也是。现在我记得为什么我不那么活跃了。问候。
-
静态方法多为regular函数,所以
bool (*isEnabledFunc)() = &MyClass<Derived>::isPresent;.
标签: c++ c++11 templates function-pointers