c++ - 未在 clang++ 中编译的具有指向方法的指针的嵌套模板类

标签 c++ templates gcc clang++

SSCCE我的问题是:

template <class T> class MyClass
{
  template <void (MyClass::*M)() const> struct wrapper
  {
    virtual void call();
  };
};

template <typename T>
template <void (MyClass<T>::*M)() const>
void MyClass<T>::wrapper<M>::call()
{
}

此代码在 gcc 中编译但失败并出现错误:

error: nested name specifier 'MyClass<T>::wrapper<M>::' for declaration does not refer into a class, class template or class template partial specialization
void MyClass<T>::wrapper<M>::call()
 ~~~~~~~~~~~~~~~~~~~~~~~~~^

在铿锵++中。 为什么?

call 类中的定义解决了这个问题,我知道。任何非指向方法的模板在任何地方都可以正常工作。 template/typename 的实验没有结果。

最佳答案

可能的解决方法:考虑使用 std::function 代替包装器。它不完全相同(对可接受的函数指针的约束较少),但它会在 clang++ 上编译并简化您的代码。

#include <functional>

template <class T> class MyClass
{
    typedef std::function<void(void) const > wrapper;
};

关于c++ - 未在 clang++ 中编译的具有指向方法的指针的嵌套模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31711926/

相关文章:

c++ - c++和g++如何处理unicode?

c++ - 析构函数调用中出现信号 11 的可能原因是什么?

c++ - 编译器独立的类名

c++ - 未使用的变量和函数的链接器错误

linux - 从 x86 交叉编译 MIPS 路由器

c++ - 在 Windows 7 上使用 CMake 安装 OpenCV 3.1.0,opencv_ffmpeg.dll 无效哈希

C++:如何在类被模板化时使用默认构造函数实例化后将输入输入到对象中

c++ - 是否可以在 C++ 中使用模板化的单参数求和函数?

c++ - 完善的转发功能推导出冲突错误

c++ - 专用功能拉开功能型