- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在我最近编写的代码中,我注意到一个奇怪的行为。
当我使用第一个参数为 std::pair
的 make_pair
时,make_pair
变得“神奇地”在命名空间中可用(我不必使用 std::
限定符)
#include <iostream>
int main()
{
int i1 = 2; int i2 = 10; int i3 = 0;
// constructing a pair using std::make_pair, everything's okay
std::pair<int,int> key = std::make_pair(i1, i2);
// here, why is make_pair suddenly magically available without the
// std:: namespace qualifier?
// At the same time, using ::make_pair yields and error
// (make_pair has not declared...)
std::pair<std::pair<int,int>, int> mypair = make_pair(key, i3);
std::cout << mypair.first.first << "\n";
std::cout << mypair.first.second << "\n";
std::cout << mypair.second << "\n";
return 0;
}
编译正常(使用 -Wall 和 -pedantic-errors
)并输出:
2
10
0
为什么会这样?我查看了 cppreference 并没有发现任何关于此行为正确的提示。我错过了什么吗?
仅供引用,我使用的是 gcc 4.6.3
最佳答案
这是 C++ 的一个鲜为人知的特性,正如 @jrok 所指出的,Koenig Lookup 非常快,或者在现代 C++ 中1),ADL(参数相关查找).它所做的基本上是在参数的 namespace 中搜索要调用的函数(在本例中为 make_pair
)。触发 ADL 的参数显然是 std::pair
.
1)命名已更改,尽管很多人都知道第一个术语
也许值得一提的是,ADL 对于一种特定类型的函数非常重要:运算符。如果没有 ADL,即使是简单的 C++“你好,世界!”也是不可能的。工作,因为这个:
std::cout << "Hello, world!";
必须这样写:
std::operator<< (std::cout, "Hello, world!");
感谢 ADL,<<
正确解析为 std
命名空间。
引用资料:
关于c++ - make_pair 命名空间污染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17293445/
我正在尝试将我的插件提交到 mozilla 站点,但我收到了这个该死的警告: 代码(在 mf_options.js 中)非常简单(我认为问题只存在于“开始存储”和“结束存储”之间: // start
精简版 在下面的代码中,$1被污染了,我不明白为什么。 长版 我在运行 Foswiki在带有 -T 的 perl v5.14.2 系统上启用污点检查模式。 调试该设置的问题,我设法构建了以下 SSCC
我有一个 Controller ,其中 $scope 对象已用于存储仅在同一 Controller 中本地使用的方法和值。有很多这样的事情发生: $scope.foo = 'something'; $
我知道将不受信任的对象标记为受污染的可能性,但根本目的是什么,我为什么要这样做? 最佳答案 将污点作为一种安全预防措施进行跟踪,以确保不受信任的数据不会被错误地用于计算、交易或解释为代码。 通过内置语
我正在开始一个新的 react 项目,我刚刚安装了非常基本的包(npx create-react-app),没有任何其他东西。当我运行审核时,我得到以下低漏洞:
我知道关于这个特定主题有很多,但是我找不到任何偶然发现我的问题的人,希望有人可以向我解释这一点。 我有一个域,我在 equals 方法中使用注入(inject)的 grailsApplication
我在程序中使用许多不同的记录,其中一些使用相同的字段名称,例如 data Customer = Customer { ..., foo :: Int, ... } data Product = Pro
我有一个包含 2 个节点组的集群:实时和常规。我只希望能够实时容忍亲和性的 Pod 能够在来自实时集群的节点上运行。 我的方法是污染相关节点并向我要注册到该节点的 pod 添加容忍度。当我试图污染一个
在其中一个回复中,BalusC 已经提到了 初学者的一个常见错误是,他们将 servletcontainer/appserver 的库复制到/WEB-INF/lib 中,因为他们在 IDE 中遇到编译
最近我偶然发现了一个vulnerability in doT.js 。该漏洞的存在是因为攻击者可以利用原型(prototype)污染来修改传入 doT 的选项值。 示例: var doT = requ
我有一个带有多个命名空间的仅 header C++ 库。 例如一个头文件可能包含 //header1.h namespace library{ namespace componentA{ tem
我正在尝试将 svg 转换为图像并提示用户下载。 var chart = $(svg.node()) .attr('xmlns', 'http://www.w3.org/200
在 javascript 中,可以“覆盖”Object.prototype 的属性或方法。例如: Object.prototype.toString = function(){ return "s
当我们要写入一个数据项时,首先将包含数据的块带入缓存,然后将数据项写入缓存。这可能会导致缓存污染。为了避免这种情况,英特尔没有引入临时指令。 如果我要使用 mmap() 将数据写入文件并且永远不会再次
我可以感染一个节点,但不能感染一个实例组。 kubectl taint nodes nodeA kops.k8s.io/instancegroup=loadbalancer:NoSchedule 下面
我的应用程序显示存储在 中的图像AWS S3 (出于安全原因,在私有(private)存储桶中)。 为了让用户从他们的浏览器中看到图像,我生成了 签名 URL 喜欢 https://s3.eu-cen
总结 对于我的 CS 顶点项目,我们遇到了一个类名存在于两个不同依赖项中的问题。具体来说,我们使用一个依赖项将 MySQL 与 Entity Frame 一起使用,一个用于直接连接和执行 MySQL
我在 VS 2005 中有一个 VC++ 项目,我制作了 32 位和 64 位版本。我刚刚安装了 VS 2010 并试图将项目移植到较新的 Visual Studio。它不会移植,因为该项目引用了平台
我正在使用 Struts 2 v 2.3.16.3 和 tomcat 6。 用户将单击一个通过 id 查找对象的操作,页面将显示它。我遇到了一个零星的错误,用户会突然从另一台机器上的另一个用户那里得到
这个问题已经有答案了: Tainted Canvas, due to CORS and SVG? (2 个回答) 已关闭 4 年前。 我在 Angular JS 中有一个指令,允许将 SVG 导出为
我是一名优秀的程序员,十分优秀!