为什么 clang 找不到在调用站点之前声明的函数?

Why clang does not find a function declared prior to the call site?

我想为 simd 类型提供一些 sqrt 包装函数,这样它们就可以从模板中与 std::sqrt 一起使用。

现在我遇到了问题,它们不知何故不可见。只能使用std中定义的。

这是代码的高度简化部分:

#include <cmath>
#include <pmmintrin.h>

using float_simd = __m128;
using double_simd = __m128d;

float_simd sqrt(float_simd a) { return _mm_sqrt_ps(a); }
double_simd sqrt(double_simd a) { return _mm_sqrt_pd(a); }

template <typename T>
void do_fancy(T val) 
{
    using std::sqrt;
    auto ret = sqrt(val);
    (void)ret;
}

int main() {
    double testDouble = 1.0; 
    float testFloat = 1.0f;
    double_simd testSimdDouble;
    float_simd testSimdFloat;
    do_fancy(testDouble);
    do_fancy(testFloat);
    do_fancy(testSimdDouble);
    do_fancy(testSimdFloat);
    return 0;
}

现在 clang 给出了这个:

main.cpp:14:16: error: call to function 'sqrt' that is neither visible in the template definition nor found by argument-dependent lookup
    auto ret = sqrt(val);
               ^
main.cpp:25:5: note: in instantiation of function template specialization 'do_fancy<__attribute__((__vector_size__(2 * sizeof(double)))) double>' requested here
    do_fancy(testSimdDouble);
    ^
main.cpp:8:13: note: 'sqrt' should be declared prior to the call site
double_simd sqrt(double_simd a) { return _mm_sqrt_pd(a); }
            ^
main.cpp:14:16: error: call to function 'sqrt' that is neither visible in the template definition nor found by argument-dependent lookup
    auto ret = sqrt(val);
               ^
main.cpp:26:5: note: in instantiation of function template specialization 'do_fancy<__attribute__((__vector_size__(4 * sizeof(float)))) float>' requested here
    do_fancy(testSimdFloat);
    ^
main.cpp:7:12: note: 'sqrt' should be declared prior to the call site
float_simd sqrt(float_simd a) { return _mm_sqrt_ps(a); }
           ^

它说,simd sqrt 功能 "should be declared prior to the call site",但我认为它们是。

我在网上搜索了一下,但不幸的是我只找到了调用和声明顺序实际上是错误的情况。

我刚刚注释掉了 using std::sqrt,一切正常。我不明白...现在如何找到 std::sqrt 个?

我在 macOS 上使用来自自制软件的 clang 3.9.1。

using std::sqrt; 在函数 body 中添加了 sqrt 的声明,因此函数的名称查找会找到该声明,而不会考虑函数外的声明 body 在封闭范围内。

这是 "name hiding" 的一种形式,它是 C++ 的一种 属性,其中一个作用域中的名称 "hide" 个在外部作用域中具有相同名称的实体。发生这种情况是因为编译器从最内层的范围开始并查找名称,然后只有在没有匹配项时才尝试封闭范围,如此直到它到达最外层(即全局)范围。因此,一旦在给定范围内找到一个或多个匹配项,它就会停止搜索名称,并且不会看到外部范围内的匹配名称。

在您的代码中,名称 sqrt 通过引用 std::sqrt 函数的 using 声明在函数 body 中声明。名称查找从该函数的范围开始 body,找到一个匹配项,而不是在周围的命名空间中查找。

您需要做的:

using std::sqrt;
using ::sqrt;

这意味着两组重载(您在全局命名空间中定义的那些,以及 <cmath> header 在命名空间 std 中定义的那些)都在函数中声明范围,两者都可以通过名称查找找到。现在编译器将在函数范围内找到所有这些重载,重载解析将根据参数类型选择最佳的。

另一种选择是将 using std::sqrt using-declaration 移动到全局命名空间,而不是在函数 body 中。这会将 std::sqrt 添加到全局命名空间,因此它不会隐藏您自己的 sqrt 重载。如果函数 body 中没有 using-declaration,则最内层范围内将没有匹配项,因此编译器将在封闭范围内查找,即全局命名空间。在该范围内,它会找到所有重载,并且重载解析将选择最好的一个。

另一种选择是将 <cmath> 替换为 <math.h> 并删除 using std::sqrt;。这将确保 sqrt 的标准库版本是在全局命名空间而不是命名空间 std 中声明的,因此您不需要 using std::sqrt; 就可以不合格地调用它。

正如您在下面的评论中指出的那样,如果您只是注释掉 using-declaration,它也可能会起作用,但是这不可移植并且不能保证起作用。它恰好适用于您使用的编译器,因为 <cmath> 声明了 std::sqrt ::sqrt (所以它等同于 using std::sqrt; 在全局命名空间中),但并非所有编译器都这样做。为保证您在全局命名空间中获得 ::sqrt,您应该将 using std::sqrt; 放入全局命名空间或包含 <math.h>.