什么是名称查找机制? [英] What is name lookup mechanism?

查看:158
本文介绍了什么是名称查找机制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道C ++名称查找机制是什么。

解决方案

名称查找是识别什么名称意味着。名称查找有两个目的




  • 消除您的代码解析

  • 确定您的代码是什么意思



例如,如果您有此代码

  T(a); 

这取决于 T 或者:如果它是一个类型,它将是一个 a 的声明,如果它不是一个类型,它被解释为一个函数调用。


某些名称表示类型或模板。通常,无论何时遇到名称,在继续解析包含它的程序之前,需要确定该名称是否表示这些实体之一。确定这一点的过程称为名称查找。



名称查找将名称的使用与该名称的声明(3.1)相关联。


有两种主要的名称查找类别




  • 未限定名称查询:从当前作用域开始,查找名称,如果在类中,则转义为包含作用域和基类。不从特定的命名范围开始。此查找窗体会在找到名称时立即停止。因此,内部作用域(或类)中的名称​​隐藏外部作用域(或基类)中找到的名称。

  • 限定名称查找:使用 :: 运算符在给定作用域中查找名称。



存在其他几种形式,例如查找点或箭头后面显示的名称(如 ptr- > foo )或在 class foo 中查找名称(其中忽略非类型名称)。一种特别有趣的形式是用于根据函数调用中使用的参数类型查找函数声明的参数依赖查找



名称查找后找到一个声明,它会检查以查看它有什么属性和程序是否可以使用它。

只有在名称查找,函数重载解析(如果适用)和访问检查成功之后才是通过名称声明引入的属性。 blockquote>

因此,名称查找将查找私有类成员,但如果您使用此类名称,如果您没有访问它们,您的代码将被拒绝。这是真的,即使基类将具有相同的名称与公共访问 - 这是因为名称查找停止在派生类,如果它找到一个名称。


I'd like to know what C++ name lookup mechanism is.

解决方案

Name lookup is the process of identifying what a name means. Name lookup has two purposes

  • Disambiguate parsing of your code
  • Determining what precisely your code means

For instance if you have this code

T(a);

It depends on whether T is a type or not: If it is a type, it will be a declaration of a, and if it isn't a type, it's interpreted as a function call.

Some names denote types or templates. In general, whenever a name is encountered it is necessary to determine whether that name denotes one of these entities before continuing to parse the program that contains it. The process that determines this is called name lookup.

Name lookup associates the use of a name with a declaration (3.1) of that name.

There are two main classes of name-lookup

  • Unqualified name lookup: Starting from the current scope, a name is looked up, escaping into the enclosing scopes and base classes if inside a class. Does not start from a specific named scope. This lookup form stops as soon as it finds a name. So a name in an inner scope (or class) hides a name found in an outer scope (or base class).
  • Qualified name lookup: Looking a name up in a given scope using the :: operator.

Several other forms exist, like looking up a name that appears after the dot or arrow (like ptr->foo) or looking up a name in class foo (in which nontype names are ignored). One particular interesting form is the argument dependent lookup used for finding function declarations based on argument types used in a function call.

After name lookup found a declaration, it's inspected to see what attributes it got and whether the program can use it.

Only after name lookup, function overload resolution (if applicable) and access checking have succeeded are the attributes introduced by the name’s declaration used further in expression processing

Thus name-lookup will find private class members, but your code is rejected if you use such names if you haven't access to them. This is true even if a base class would have the same name with public access - that's because name-lookup stops in the derived class if it finds a name.

这篇关于什么是名称查找机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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