c – 在模板中转换成员函数指针
假设我有以下两个类:
template<typename T> struct Base { void foo(); }; struct Derived : Base<Derived> {}; 我可以做这个: void (Derived::*thing)() = &Derived::foo; 编译器很高兴(正如我所料). 当我把它放在两个级别的模板中时突然爆炸: template<typename T,T thing> struct bar {}; template<typename T> void foo() { bar<void (T::*)(),&T::foo>{}; } int main() { foo<Derived>(); // ERROR foo<Base<Derived>>(); // Works fine } 这失败了: non-type template argument of type 'void (Base<Derived>::*)()' cannot be converted to a value of type 'void (Derived::*)()' godbolt 为什么简单的案例工作而更复杂的案例失败了?我相信这与this问题有关,但我并不完全确定…. 解决方法
@YSC钉出了& Derived :: foo;的类型.既然你想知道为什么这个隐式转换……
void (Derived::*thing)() = &Derived::foo; …飞行正常但不在模板中,原因如下:
我省略的列表不包含pointer to member conversions.因此,使该模板参数对您指定的参数无效. 一个简单的解决方法是使用decltype(& T :: foo)而不是void(T :: *)()作为类型参数.这是一个结构良好的替代品: bar<decltype(&T::foo),&T::foo>{}; 无论是否可接受,当然取决于您的用例,超出了MCVE的范围. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |