C ++方法调用和类型范围解析的歧义

编程入门 行业动态 更新时间:2024-10-25 20:19:07
本文介绍了C ++方法调用和类型范围解析的歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望标题实际上描述了我想问的问题...

I hope the title actually describes what I wanted to ask...

我写了一段代码,可以用gcc编译并按我的预期工作.但是,它不能使用llvm进行编译,并且使用icc进行编译时代码的执行方式也不同! 这是问题的一个示例:

I wrote a piece of code that compiles with gcc and works as I intended. However, it does not compile with llvm and the code executes differently when compiled with icc! Here is an example of the problem:

#include <iostream> using std::cout; using std::endl; class A { public: virtual void foo() { cout << "A::foo()" << endl; } }; class B : public A { public: typedef A base; virtual void foo() { cout << "B::foo()" << endl; } }; int main() { typedef B base; base* bp = new B(); bp->base::foo(); }

gcc输出:A :: foo() icc输出:B :: foo()

gcc output: A::foo() icc output: B::foo()

有人可以解释一下标准对此案的看法吗?

Could somebody explain what does the standard say about this case?

推荐答案

摘自C ++ 11,第3.4.5/4节:

From C++11, §3.4.5/4:

如果类成员访问中的id-expression是 形式 If the id-expression in a class member access is a qualified-id of the form class-name-or-namespace-name::...

类名或命名空间名 跟随.或->运算符首先在 对象表达式和名称(如果找到).否则是 在整个后缀表达式的上下文中查找.

the class-name-or-namespace-name following the . or -> operator is first looked up in the class of the object expression and the name, if found, is used. Otherwise it is looked up in the context of the entire postfix-expression.

我认为这不会更清楚.找到B::base,因此输出 应该是A::foo().

I don't think it can be clearer. This finds B::base, so the output should be A::foo().

更多推荐

C ++方法调用和类型范围解析的歧义

本文发布于:2023-11-07 06:46:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565801.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:歧义   类型   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!