没有在clang++中编译带有指针到方法的嵌套模板类
Nested template classes with pointer-to-method not compiled in 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()
~~~~~~~~~~~~~~~~~~~~~~~~~^
在叮当声+ +。为什么?
在类调用定义解决了问题,我知道。任何非指针方法模板在任何地方都可以正常工作。使用模板/typename进行实验没有结果。
可能的解决方法:考虑使用std::function代替wrapper。它不完全相同(对可接受的函数指针的限制较少),但它可以在clang++上编译并简化您的代码。
#include <functional>
template <class T> class MyClass
{
typedef std::function<void(void) const > wrapper;
};
您可以在类定义中移动方法的实现:
template <class T> class MyClass
{
template <void (MyClass::*M)() const> struct wrapper
{
virtual void call(){/* move implementation here */};
};
};
相关文章:
- 单独定义模板化嵌套类方法的正确语法
- 在 c++ 中实现嵌套循环的更短方法吗?
- 如何在该接收类之外声明嵌套类的方法
- 从列表/向量制作嵌套 for 循环的最佳方法是什么?
- 如何重写继承的嵌套类中存在的虚拟方法
- CRTP:为什么获得嵌套类型和派生类的嵌套方法有区别
- 在 C 中并行化嵌套循环的几种方法之间的差异,C++使用 OpenMP
- 嵌套类的替代方法
- 许多变量,没有嵌套循环的最佳方法
- 在嵌套名称空间范围中使用名称空间的正确方法
- C++:将 Args && ... _args传递到嵌套列表的正确方法?
- 如何使用new()在嵌套类中调用方法
- 执行三个嵌套for循环的最快方法是什么
- 模板化方法中使用的嵌套类的未解析外部符号
- 覆盖我正在扩展的嵌套类的方法
- 定义类(和嵌套类)成员函数的更好/更整洁/更可读的方法
- 简单C++是嵌套布尔方法
- 静态嵌套bool会帮助我禁用对某些类型的调用吗?或者有更干净的方法吗
- 有没有一种方法可以将宏名称作为参数传递给嵌套宏,而在最外层宏展开时不展开它们
- 执行方法调用时,有什么理由不C++中添加嵌套类/结构解析