澄清C 名称查找

Clarification on C++ name lookup

本文关键字:查找 澄清      更新时间:2023-10-16

我对刚刚上班的情况有一个疑问。

设置:在StringStuff.h

namespace n1
{
namespace n2
{
    typedef std::string myString;
}
}
namespace n1
{
namespace n2
{
    void LTrim(myString& io_string);
    void RTrim(myString& io_string);
    inline void Trim(myString& io_string)
    {
        LTrim(io_string);
        RTrim(io_string);
    }
}
}

在inmell.cpp

#include "stringStuff.h" // This actually gets included via other include files
#include "globalInclude.h" // contains 'using namespace n1::n2;'. Yes, I know this isn't best practice
inline static myString Trim(const myString& in_string)
{
    // impl
}
static void impl(const myString& in_string, myString& out_string)
{
    size_t N = 10; // just some computed value.
    out_string = Trim(in_string.substr(1, N)); // This is the line with the error
}

现在,我承认我不太了解C 的姓名分辨率规则,但是当我看这个问题时,似乎是Trim 应该的呼吁是模棱两可的。

令人惊讶的是,它使用GCC在Linux上恰好编译,调用Inmell.CPP中定义的函数。当我使用其本机编译器在HP-UX上编译时,它似乎可以解决呼叫作为StringStuff.h中定义的呼叫,并抱怨将临时性转换为非const Ref(这是一个令人惊讶的警告,而不是错误),以及试图将空隙分配给Mystring。

根据C 标准应该发生什么,哪个编译器(如果两者都正确)?

顺便说一句,我在我的情况下"修复"了它,通过将call与::的调用前相结合,尽管这并不是真正的理想。

的诀窍是,一个是一个const引用,而另一个则是非const参考,因此它们是两个不同的函数,而不是同一函数,其分辨率可能模棱两可。然后归结为呼叫中的参数类型。由于substr返回一个值,因此它只能绑定到const引用。因此,它应该在没有警告的情况下致电::Trim。我会说HP-UX编译器是错误的。