【问题标题】:How does 'using' directive work with template member functions'using' 指令如何与模板成员函数一起使用
【发布时间】:2014-02-26 19:27:01
【问题描述】:

我正在使用 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 declaration for type-dependent template name(包括 cmets)表明这是不可能的,并且可能是标准中的疏忽。

【问题讨论】:

    标签: c++ templates crtp using-directives


    【解决方案1】:

    我不知道如何解决using 语句的问题(它应该类似于using A&lt;C&lt;T&gt;&gt;::template g;,但这段代码不能用我的编译器编译)。但是您可以通过以下方式之一调用g&lt;void&gt; 方法:

    • this-&gt;template g&lt;void&gt;()

    • A&lt;C&lt;T&gt;&gt;::template g&lt;void&gt;()

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

    【讨论】:

    • 是的,你的另外两个语句是我在不使用using 的情况下访问g 的方式,但是它们都很麻烦!我希望template 的魔法咒语能让我避开它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多