7

以下に 2 つのケースを示します。

ケース 1) Base->BaseIndirect->DerivedIndirect

ケース 2) ベース -> 派生

ケース 2) では、3 つの表記法を使用して Base クラスのテンプレート関数を呼び出すことができます。ケース 1) では、これらの表記のうち 1 つだけを使用して Base クラスのテンプレート関数を呼び出すことができます。そして、表記法を使用して BaseIndirect のテンプレート関数を呼び出すことができません:(。これを修正するにはどうすればよいですか?ありがとう。

struct Base {
  template<bool R> inline void fbase(int k) {};
};

template<class ZZ> struct BaseIndirect : Base {
  template<bool R> inline void fbaseIndirect(int k) {};
};


template<class ZZ>
struct DerivedIndirect : BaseIndirect<ZZ> {
  DerivedIndirect() {
    this->fbase<true>(5);         // gives error, line 13
    fbase<true>(5);               // gives error, line 14
    Base::fbase<true>(5);           // WORKS, line 15
    this->fbaseIndirect<true>(5); // gives error, line 16
    fbaseIndirect<true>(5);       // gives error, line 17
    BaseIndirect<ZZ>::fbaseIndirect<true>(5);   // gives error, line 18
  }
};

template<class ZZ>
struct Derived : Base {
  Derived() {
    this->fbase<true>(5); //  WORKS
    fbase<true>(5);       // WORKS
    Base::fbase<true>(5); // WORKS
  }
};


int main() {
  Derived<int> der;
  DerivedIndirect<int> derIndirect;
};                              

コンパイル時のエラー

test.cpp: In constructor 'DerivedIndirect<ZZ>::DerivedIndirect()':
test.cpp:14: error: 'fbase' was not declared in this scope
test.cpp:17: error: 'fbaseIndirect' was not declared in this scope
test.cpp: In constructor 'DerivedIndirect<ZZ>::DerivedIndirect() [with ZZ = int]':
test.cpp:34:   instantiated from herep 
test.cpp:13: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<'
test.cpp:16: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<'
test.cpp:18: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<'
4

1 に答える 1

14

templateこれらの呼び出しの多くが失敗する理由は、キーワードの最もあいまいな使用法を 1 つ使用して解決する必要がある構文上のあいまいさがあるためです。書く代わりに

this->fbase<true>(5);

あなたは書く必要があります

this->template fbase<true>(5);

その理由は、templateキーワードがない場合、コンパイラはこれを次のように解析するためです。

(((this->fbase) < true) > 5)

これは無意味です。template キーワードは、このあいまいさを明示的に取り除きます。templateあなたが言及した他のケースにキーワードを追加すると、これらの問題が修正されるはずです。

これが直接の基本クラスで機能する理由が実際にはわからないので、誰かが質問のその部分に答えることができれば、答えが何であるかを知りたい.

于 2011-02-08T05:25:57.513 に答える