C++中的不合格查找
Unqualified lookup in C++
#include <stdio.h>
#include <cstddef>
#include <cstring>
namespace /*namespace name generated by compiler*/
{
struct BB{};
}
struct AA{};
namespace my
{
inline void * memcpy(void*, const void*, std::size_t)
{
puts("CUSTOM IMPLEMENTATION");
return 0;
}
}
namespace my
{
void func()
{
AA a;
memcpy(&a, &a, sizeof(a)); // ambigious call for g++4.7 - g++6.2
BB b;
memcpy(&b, &b, sizeof(b)); // unambigious call
}
}
int main(int, char **)
{
my::func();
return 0;
}
为什么memcpy在这里是模棱两可的电话?
请查看ANSI ISO IEC 14882, C++2003, 3.4.1, (6( (第30页(中变量"i"的示例。它"证明"这种结构没有歧义。
namespace A {
namespace N {
void f();
}
}
void A::N::f() {
i = 5;
// The following scopes are searched for a declaration of i:
// 1) outermost block scope of A::N::f, before the use of i
// 2) scope of namespace N
// 3) scope of namespace A
// 4) global scope, before the definition of A::N::f
}
是 GCC 中违反了不合格的查找规则,还是我不明白什么?
要查找的名称是函数名称;特殊的与参数相关的查找规则在此处生效。(请注意,ADL 是函数名称的非限定名称查找的一部分。
除了通常的非限定名称查找所考虑的范围和命名空间之外,还会在其参数的命名空间中查找这些函数名称。
首先,您包括string.h
,它在全局命名空间中引入了名称memcpy
。
并且AA
是在全局命名空间中声明的;然后当你调用memcpy(&a, &a, sizeof(a));
时,声明AA
的命名空间(即全局命名空间(也会被考虑在内,并且在命名空间my
中声明的memcpy
将通过通常的非限定名称查找找到,因此调用是不明确的。
另一方面,BB
没有这样的问题,因为它没有在全局命名空间中声明(然后 ADL 不会对其生效(。
相关文章:
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 方法内部但循环仍得到预期的不合格id错误C++
- g++ -I 和 -L 命令来查找不起作用的 .hpp 文件
- 查找不存在的键时,unordered_map返回什么
- 如果我注释掉换行符,为什么'string'会成为一个不合格的变量
- set::find 查找不存在的元素
- 查找不在标准中的第一个值::设置<int>最小-最大值
- C++中的不合格查找
- 运算符<<依赖于参数的查找不在全局命名空间中查找
- 在不合格的呼叫上,Clang和GCC上的错误
- 在libclang中获取类型不合格的版本
- C 预期的不合格ID(类继承)
- 错误:ISO C 禁止采用不合格或括号的非静态成员函数的地址,以形成指向成员函数的指针
- 使用 windbg 查找不在堆栈顶部的函数的函数参数
- c++:模板中不合格名称查找的上下文
- 解析问题:预期的不合格的ID和语义问题:C 需要所有声明的类型说明符
- 通常的不合格查找和与参数有关的名称查找(ADL)
- 名称查找:在类定义中使用不合格类名/省略模板参数的缺点
- 帮助,澄清我对不合格名称查找的疑问:ISO n3290草案
- 不合格名称的名称查找:C++0x草案n3290