c++ - 'using'指令如何与模板成员函数一起工作

标签 c++ templates crtp using-directives

我正在使用 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/

相关文章:

c++ - 如何在 SWIG 中包装可变模板类的可变模板成员函数?

c++ - 正则表达式检测 typedef/class 关键字之后的任何内容?

c++ - 可以在 C++ catch(...) block 中访问抛出的异常吗

c++ - 是否可以根据类模板的参数来调用类方法和全局方法?

c++ - python中具有静态成员的模板上的swig undefined symbol

c++ - CRTP vs 过载和最终

c++ - 在 gcc 8.2 而非 MSVC 19 中编译的模板代码

c++ - 头文件中定义的函数原型(prototype)错误

模板化类中的 C++ 模板成员变量类型不完整

c++ - CRTP 的模糊过载