"using"指令如何使用模板成员函数

How does 'using' directive work with template member functions

本文关键字:成员 函数 何使用 using 指令      更新时间:2023-10-16

我正在使用CRTP,基类有一个模板函数。如何在模板派生类中use该成员函数?

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语句解决问题(它应该看起来像using A<C<T>>::template g;,但这段代码不能用我的编译器编译)。但是,您可以通过以下方式之一调用g<void>方法:

  • this->template g<void>()

  • A<C<T>>::template g<void>()

有关使用关键字的阴暗面的详细信息template请参阅此问题的答案。