本文介绍了C ++调用Base类的模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是两种情况。

案例1)Base-> BaseIndirect-> DerivedIndirect

Case 1) Base->BaseIndirect->DerivedIndirect

案例2)Base-> Derived

Case 2) Base->Derived

在情况2),我能够使用3个符号调用Base类的模板函数。
在case 1),我能够调用基类的模板函数,只使用这些符号中的一个。而且,我不能调用BaseIndirect的模板函数使用任何符号:(。我如何解决这个问题?

In Case 2), I am able to call a template function of Base class using 3 notations.In Case 1), I am able to call template function of Base class using only 1 of those notations. And, I am NOT able to call template function of BaseIndirect using any notation :(. How do I fix this? Thanks.

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. Instead of writing

this->fbase<true>(5);

您需要写

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

原因是没有模板关键字,编译器将此解析为

The reason is that without the template keyword, the compiler parses this as

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

这是无意义的。模板关键字显式地删除了这种模糊性。将模板关键字添加到您提到的其他情况下应该可以解决这些问题。

Which is nonsensical. The template keyword explicitly removes this ambiguity. Adding the template keyword into the other cases you mentioned should fix those problems.

这适用于直接基类,所以如果有人可以回答这个问题的部分,我想看看答案是什么。

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.

这篇关于C ++调用Base类的模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:18