C ++名称解析(和重载)规则列表 [英] List of C++ name resolution (and overloading) rules

查看:144
本文介绍了C ++名称解析(和重载)规则列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里可以找到一个规则,一个C ++兼容的编译器必须应用以执行名称解析(包括重载)的列表?



我想要一个像自然语言算法或流程图。



C ++标准当然有这套规则,但它被建立为新的语言语句并且其结果很难记住。



为了做一个长的故事,我想知道这个问题的完整和详细的答案



我知道C ++是所有的我们这样做,当X而不是Y

EDIT :如果Z持有,我会询问是否可以使其更加线性。工作在这个主题的草案,可以改进集体一次发布。然而,我今天很忙,可能需要时间有一些可宣传。如果有人感兴趣,我会推广原始txt文件的个人笔记到更好的东西,并发布。

解决方案

大致如下:




  • 如果名称前有 :: ,如 :: A X :: A ,然后使用限定名称查找。首先查找 X ,如果存在(如果不使用全局命名空间),然后查看 A 。如果 X 是一个类,并且 A 不是直接成员, c $ c> X 。如果在多个基中找到 A ,则失败。


  • 否则,作为 A(X)的函数调用,使用与参数相关的查找。这是困难的部分。在命名空间中查找 A 的朋友中声明 X 的类型, X ,并且如果 X 是模板实例化,同样对于所有涉及的参数。仅由 typedef 关联的范围不适用。


  • 如果参数相关查找不适用,请从无限定查找开始。这是找到变量的常用方法。从当前范围开始,向外工作,直到找到名称。注意,这方面使用命名空间指令,其他两种情况不会这样。



b $ b

只要看一下标准,就会发现许多例外和弊端。例如,在使用ADL来生成潜在过载列表之前,使用非限定查找来确定是否将该名称用作函数调用,而不是cast-expression。非限定查找不会在嵌套的局部类的封闭范围中查找对象,因为这样的对象在引用时可能不存在。



应用常识,更具体的问题何时(通常情况下)直觉失败。


Where I can find a list of the rules that a C++ compliant compiler must apply in order to perform names resolution (including overloading)?

I'd like something like a natural-language algorithm or flow chart.

C++ standard of course has this set of rules but it is build up as new language statements are introduced and the result it's pretty hard to remember.

To make a long story short, I'd like to know the complete and detailed answer to the question "What compiler do when it see the name 'A'?"

I know C++ is all "We do this when X but not Y if Z holds" so, I'm asking whether it is possible to make it more linear.

EDIT: I'm working on a draft of this topic, something that may be improved collectively once posted. However i'm very busy this days and it may take time to have something publicable. If someone interested i'll promote the "personal note on a raw txt file" to something better and post it.

解决方案

Well, in broad strokes:

  • If the name is preceded by ::, as in ::A or X::A, then use qualified name lookup. First look up X, if it exists (if not use the global namespace) then look inside it for A. If X is a class, and A is not a direct member, then look in all the direct bases of X. If A is found in more than one base, fail.

  • Otherwise, if the name is used as a function call such as A( X ), use argument-dependent lookup. This is the hard part. Look for A in the namespace the type of X was declared in, in the friends of X, and if X is a template instantiation, likewise for all the arguments involved. Scopes associated only by typedef do not apply. Do this in addition to unqualified lookup.

  • Start with unqualified lookup if argument-dependent lookup doesn't apply. This is the usual way variables are found. Start at the current scope and work outwards until the name is found. Note that this respects using namespace directives, which the other two cases do not.

Simply glancing at the Standard will reveal many exceptions and gotchas. For example, unqualified lookup is used to determine whether the name is used as a function call, as opposed to a cast-expression, before ADL is used to generate a list of potential overloads. Unqualified lookup doesn't look for objects in enclosing scopes of nested of local classes, because such objects might not exist at the time of reference.

Apply common sense, and ask more specific questions when (as often it does) intuition fails.

这篇关于C ++名称解析(和重载)规则列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆