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

发布于 2024-10-16 12:18:21 字数 2028 浏览 1 评论 0原文

下面是两个案例。

情况 1) Base->BaseIndirect->DerivedIndirect

情况 2) Base->Derived

在情况 2) 中,我能够使用 3 个符号调用 Base 类的模板函数。 在情况 1) 中,我可以仅使用其中一种符号来调用基类的模板函数。而且,我无法使用任何符号调用 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<'

Below are two cases.

Case 1) Base->BaseIndirect->DerivedIndirect

Case 2) Base->Derived

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪少女 2024-10-23 12:18:21

其中许多调用失败的原因是,您需要使用 template 关键字的最晦涩的用法来解决语法歧义。而不是写

this->fbase<true>(5);

你需要写

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

The 原因是,如果没有 template 关键字,编译器会将其解析为 这

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

是无意义的。 template 关键字明确消除了这种歧义。将 template 关键字添加到您提到的其他情况中应该可以解决这些问题。

我实际上不确定为什么这适用于直接基类,所以如果有人可以回答问题的这一部分,我很想看看答案是什么。

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);

You need to write

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文