Name lookup Clarification

巧了我就是萌 提交于 2019-12-04 17:51:39

Q1: I think so.

Q2: Compatibility with C. When you declare a struct in C, the tag name is just that, a tag name. To be able to use it in a standalone way, you need a typedef. In C++ you don't need the typedef, that makes live easier. But C++ rules have been complicated by the need to be able to import already existing C headers which "overloaded" the tag name with a function name. The canonical example of that is the Unix stat() function which uses a struct stat* as argument.

Q3: Standard reading is usually quite difficult... you need to already know that there is no place elsewhere modifying what you are reading. It isn't strange that people knowing how to do that are language lawyer...

You are mistaken about the second comment. In S::x, the S is a name in a nested name specifier. What the Standard refers to with "base-specifier" is the following

namespace B { struct X { }; void X() }
struct A : B::X { }; // B::X is a base-specifier

You are also not correct about this:

::S(); // nested name specifier, ignores the struct declaration 'S'.`

That code calls the function not because ::S would be a nested-name-specifier (it isn't a nested-name-specifier!), but because function names hide class or enumeration names if both the function and the class/enumeration are declared in the same scope.

FWIW, the following code would be equally valid for line 2 of your main

p->S::f();

What's important is that S preceedes a ::, which makes lookup ignore the function. That you put :: before S has no effect in your case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!