简体   繁体   English

C ++调用Base类的模板函数

[英]C++ calling template functions of Base class

Below are two cases. 以下是两个案例。

Case 1) Base->BaseIndirect->DerivedIndirect 案例1)Base-> BaseIndirect-> DerivedIndirect

Case 2) Base->Derived 案例2)Base-> Derived

In Case 2), I am able to call a template function of Base class using 3 notations. 在案例2)中,我能够使用3种符号调用Base类的模板函数。 In Case 1), I am able to call template function of Base class using only 1 of those notations. 在案例1)中,我只能使用其中一个符号来调用Base类的模板函数。 And, I am NOT able to call template function of BaseIndirect using any notation :(. How do I fix this? Thanks. 并且,我无法使用任何符号调用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;
};                              

ERRORS on compilation 编译错误

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<'

The reason that many of these calls are failing is that there's a syntactic ambiguity you need to resolve using the single most obscure use of the template keyword. 其中许多调用失败的原因是,您需要使用template关键字的最简单模糊来解决语法歧义。 Instead of writing 而不是写作

this->fbase<true>(5);

You need to write 你需要写

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

The reason is that without the template keyword, the compiler parses this as 原因是如果没有template关键字,编译器会将其解析为

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

Which is nonsensical. 这是荒谬的。 The template keyword explicitly removes this ambiguity. template关键字明确消除了这种歧义。 Adding the template keyword into the other cases you mentioned should fix those problems. template关键字添加到您提到的其他情况中应该可以解决这些问题。

I'm actually not sure why this works for direct base classes, so if someone could answer that part of the question I'd love to see what the answer is. 我实际上不确定为什么这适用于直接基类,所以如果有人能够回答问题的这一部分,我很乐意看到答案是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM