- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在c++中有两种隐藏的名字:
1) 普通名称隐藏:[basic.scope.hiding]p1 ( http://eel.is/c++draft/basic.scope.hiding#1 ):
A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class ([class.member.lookup]).
2)隐藏在[basic.scope.hiding]p2(http://eel.is/c++draft/basic.scope.hiding#2)中的特殊名称类型:
A class name ([class.name]) or enumeration name ([dcl.enum]) can be hidden by the name of a variable, data member, function, or enumerator declared in the same scope. If a class or enumeration name and a variable, data member, function, or enumerator are declared in the same scope (in any order) with the same name, the class or enumeration name is hidden wherever the variable, data member, function, or enumerator name is visible.
我很想知道在执行非限定名称查找时名称隐藏如何与 using 指令交互。
对于第一种名称,隐藏行为非常清楚。这是因为 [basic.scope.hiding]p1 已根据 [basic.lookup.unqual] ( http://eel.is/c++draft/basic.lookup.unqual ) 部分中的规则重新表述
对于第二种名称隐藏,还没有做同样的事情。那么现在出现以下问题:
*) 这第二种类型的名称隐藏应该如何与涉及使用指令的非限定名称查找交互?
我在标准的其他地方找到 [namespace.udir]p2 ( http://eel.is/c++draft/namespace.udir#2),我认为这是回答这个问题的关键:
A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. During unqualified name lookup ([basic.lookup.unqual]), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace. [ Note: In this context, “contains” means “contains directly or indirectly”. — end note ]
将此规则的 as if 部分应用于 [basic.scope.hiding]p1 与 [basic.lookup.unqual] 部分中的规则保持一致。此应用程序也符合 [basic.scope.hiding]p4 ( http://eel.is/c++draft/basic.scope.hiding#4 ) 所以这看起来很有希望。
因此,我认为我们可以通过类似地将 [namespace.udir]p2 的 as if 部分应用于 [basic.scope.hiding]p2 来回答问题 *)。此应用程序也与 [basic.scope.hiding]p4 一致。我认为这也是对c++标准最自然、最不复杂的解释。
然而,问题在于 Clang 和 GCC 的解释与我不同。例如:
namespace N { static int i = 1; }
namespace M { struct i {}; }
using namespace M;
using namespace N;
int main() { sizeof(i); }
根据我的解释,这个程序应该是良构的,i
应该作为整型变量来查找。 Clang 和 GCC 都不同意这一点,因为名称查找具有歧义。
在 Clang 的情况下,这种更复杂的解释会导致以下错误:
namespace N { static int i = 1; }
namespace M { struct i {}; }
namespace P {
using N::i;
using M::i;
}
namespace Q { using M::i; }
using namespace P;
using namespace Q;
int main() { sizeof (i); }
没有报错,但有变化
using namespace P;
using namespace Q;
进入
using namespace Q;
using namespace P;
我们得到名称查找歧义错误。 GCC 至少在这里是一致的。
我是否正确解释了 C++ 标准?
最佳答案
我认为这里的关键短语是:
A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class (10.2).
A class name (9.1) or enumeration name (7.2) can be hidden by the name of a variable, data member, function, or enumerator declared in the same scope.
在这个例子中:
namespace N { static int i = 1; }
namespace M { struct i {}; }
using namespace M;
using namespace N;
int main() { sizeof(i); }
两个 i
都在不同的非嵌套范围内声明,因此没有隐藏。名称查找找到它们时,就好像它们是在 ::
中声明的,但这不是隐藏规则所规定的。
否则,我们有,来自 [basic.lookup]:
Name lookup shall find an unambiguous declaration for the name (see 10.2). Name lookup may associate more than one declaration with a name if it finds the name to be a function name;
::
中没有明确的声明,所以这段代码格式错误,错误正确。另一个示例也是如此,因此存在一些 using-declaration clang 编译它的顺序的事实是一个错误。
虽然这是非规范的,但 [namespace.udir] 中有一个示例可以清楚地说明这种解释:
[ Note: In particular, the name of a variable, function or enumerator does not hide the name of a class or enumeration declared in a different namespace. For example,
namespace A {
class X { };
extern "C" int g();
extern "C++" int h();
}
namespace B {
void X(int);
extern "C" int g();
extern "C++" int h(int);
}
using namespace A;
using namespace B;
void f() {
X(1); // error: name X found in two namespaces
g(); // OK: name g refers to the same entity
h(); // OK: overload resolution selects A::h
}—end note ]
关于c++ - 当非限定名称查找涉及 using-directives 时 [basic.scope.hiding]p2 的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31702956/
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我有一个在 ab 时间内运行的算法,其中 a 和 b 都是单独的输入。 我的算法还是多项式时间复杂度算法还是nn?我认为 nn 不是多项式,但我仍然不确定。 我看到 n 算法的阶乘仍然评估为 nn 复
这个问题在这里已经有了答案: Logical operators (AND, OR) with NA, TRUE and FALSE (2 个回答) 1年前关闭。 由于“is.na(NA)”返回真,“
假设我有一个具有以下结构的 Pandas 数据框: df = pd.DataFrame(dict(a=["x", "x", "y"], b=[0, 1, 1], c=[1, 2, 2])) 我想按 a
谁能帮我处理一些相当复杂的 Django 查询? 这些是我的模型: class County(models.Model): name = models.CharField(max_length
我想从某个表中选择一行并根据另一个表对结果进行排序。 这是我的表: lang1_words: word_id - word statuses: word_id - status 在每个表中 word_
我是单元测试的新手,所以请对我宽容一些。我有一些查询 RESTful API 的模块。我发现在每个测试套件中,我都使用几行代码来启动一个简单的 ExpressJS Web 服务器,以模拟一些我可以从测
假设我有以下代码: var blinker = function(element){ if(stopped){ return; } else { var sampleMappi
我正在用 JavaScript 制作一个选择你自己的冒险风格的游戏,在本节中: evade = prompt("Go out of your way to avoid them, just in ca
我的代码: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Events1 extends
我正在使用 scipy ode 来解决钟摆问题。 from scipy import * import matplotlib.pyplot as plt from scipy.integrate im
我有一个 Google 表格,用于收集客户的注册数据。收集的数据包括学生的姓名、学生选择参加的类(class)以及信用卡号。提交后,我会收到通知。收到通知后,我会转到我的 Google 表格并从信用卡
我需要定义一个操作的两个版本,定义略有不同。它是一系列包含Nat指数的成分。 open import Data.Nat data Hom : ℕ → ℕ → Set where id : (
我正在研究游戏引擎 http://ducttape-dev.org使用 boost 作为依赖项之一。有一天,当我正在编写一个链接到我的游戏引擎的测试应用程序时,OgreProcedural 的 Ext
我正在 Android 中制作一个表达式计算器,所以我想在实际计算答案之前检查字符串是否符合有效表达式的条件。 我在 Java 中试过这个正则表达式: ^\s*([-+]?)(\d+)(?:\s*([
我有以下 postgresql 查询(为便于阅读而简化): select * from a_view where a in (select * from a_function(a_input))
我开始更好地掌握 PostgreSQL 索引,但我遇到了 OR 条件的问题,我不知道如何优化我的索引以加快查询速度。 我有 6 个条件,当单独运行时,它们的成本似乎很小。下面是修剪查询的示例,包括查询
有谁知道为什么下面的代码接受诸如123-123-1234这样的答案: [1-9]\\d{2}-[1-9]\\d{2}-\\d{4} 我想到了代码,它只接受先接受 2 个数字,再接受 2 个数字,然后再
在使用 Java 1.8u40 打开带有提示类型和附加的 StringConverter 的组合框时,我遇到了以下错误。这可以追溯到执行 FXML 的团队留下的示例字符串,与 Controller 中
在 MySQL 中,我有三个不同的数据库 - 我们将它们称为 A、B 和 C。 是否可以执行涉及所有三个数据库(A、B、C)中的表的事务? (所有数据库都在同一服务器上) 最佳答案 是的,你可以。这是
我是一名优秀的程序员,十分优秀!