c++ - 将函数指针成员模板限制为仅派生类

标签 c++ function c++11 templates member

我试图将模板推导限制为仅来自同一层次结构的对象。

下面的代码可以编译

template<class T>
void RegisterAction(const string& actionId, bool(T::*f)())
{
    m_actions.emplace(actionId, std::bind(f, static_cast<T*>(this)));
}

但是这段代码没有

template<class T, typename std::enable_if_t<std::is_base_of<BaseClass, T>::value>>
void RegisterAction(const string& actionId, bool(T::*f)())
{
    m_actions.emplace(actionId, std::bind(f, static_cast<T*>(this)));
}

m_actions 的类型是 std::unordered_map<string, std::function<bool()>>

这是来自 Visual Studio 的错误

'BaseClass::RegisterAction': no matching overloaded function found
error C2783: 'void BaseClass::RegisterAction(const string &,bool (__cdecl T::* )(void))': could not deduce template argument for '__formal'

这是您将如何使用该方法:

void DerivedClass::InitActions()
{
    RegisterAction("general.copy", &DerivedClass::OnCopy);
    RegisterAction("general.paste", &DerivedClass::OnPaste);
}

顺便说一句,我不能使用 static_assert因为我正在使用 c++14。

没有人知道吗?

最佳答案

您试图引入一个新的模板参数以引起替换错误——这是正确的——但您的语法有点不正确。你应该写的是:

template<class T, typename = std::enable_if_t<std::is_base_of<BaseClass, T>::value>>
                       // ^^^ this equal sign is crucial

当你写 typename = foo ,你正在声明一个未命名的类型模板参数(这就像写 typename unused = foo )并为该类型设置默认值 foo .因此,如果有人试图用 T 实例化此模板不是来自 BaseClass ,默认参数发生替换失败,导致推导失败。

因为你写的时候没有等号,typename std::enable_if_t<...>被解释为 typename-specifier,也就是说,编译器认为您正在声明一个类型为 typename std::enable_if_t<...>非类型 模板参数,您没有命名。因此,当 T源自 BaseClass , 这个模板参数的类型是 void .由于非类型模板参数不能具有类型 void (因为没有 void 类型的值),此处发生 SFINAE 错误。

有趣的是,GCC 和 Clang 也是 fail to give a useful error message .他们还提示无法推导出未命名的模板参数,而不是指出 void非类型模板参数无效(或者甚至只是指出它类型为 void 的非类型模板参数)。

关于c++ - 将函数指针成员模板限制为仅派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55186837/

相关文章:

c++ - 调用同名函数

c++ - Initializer_list 作为非模板上下文中数组引用参数的参数

c++11 - ios_base::sync_with_stdio(false) 会影响 <fstream> 吗?

c++ - 将 BMP 加载到 OpenGL 纹理中会切换红色和蓝色。 (C++/Windows )

c++ - 在编译时检测 Visual Studio 构建工具版本

c++ - Q_IMPORT_PLUGIN 使用 qgif 和 qjpeg 插件编译错误

c++ - 如何遍历列表函数?

c++ - GMP mpz_class 数组不可用?

sql-server - "Adding a value to a ' 日期时间 ' column caused an overflow."

c++11 - O(log n)中的c++ bitset逻辑运算?