我正在使用 CRTP,基类有一个模板函数。我如何在模板派生类中使用
该成员函数?
template <typename T>
struct A {
int f();
template <typename S>
int g();
};
struct B: public A<B> {
int h() { return f() + g<void>(); } // ok
};
template <typename T>
struct C: public A<C<T>> {
// must 'use' to get without qualifying with this->
using A<C<T>>::f; // ok
using A<C<T>>::g; // nope
int h() { return f() + g<void>(); } // doesn't work
};
* 编辑 * 一个较早的问题,Using declaration for type-dependent template name ,包括评论,表明这是不可能的,可能是对标准的疏忽。
最佳答案
我不知道如何解决 using
的问题声明(它应该看起来像 using A<C<T>>::template g;
,但这段代码不能用我的编译器编译)。但您可以调用g<void>
通过以下方式之一进行方法:
this->template g<void>()
A<C<T>>::template g<void>()
查看 this question 的答案有关使用 template
黑暗面的详细信息关键字。
关于c++ - 'using'指令如何与模板成员函数一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22051261/