我希望标题实际上描述了我想问的问题...
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 ++方法调用和类型范围解析的歧义
发布评论