C++中的不合格查找

Unqualified lookup in C++

本文关键字:查找 不合格 C++      更新时间:2023-10-16
#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 不会对其生效(。