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

How does 'using' directive work with template member functions

提问人:pythonic metaphor 提问时间:2/27/2014 最后编辑:Communitypythonic metaphor 更新时间:2/27/2014 访问量:1319

问:

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

*编辑*前面的一个问题,对类型相关的模板名称使用声明,包括注释,表明这是不可能的,可能是标准中的疏忽。

C++ 模板 crtp using-directives

评论


答:

2赞 Constructor 2/27/2014 #1

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

  • this->template g<void>()

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

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

评论

0赞 pythonic metaphor 2/27/2014
是的,你的另外两个语句是我如何在不使用 的情况下访问 g,但它们都非常麻烦!我希望有一个神奇的咒语让我避开他们。usingtemplate