我有一个关于我刚在工作中遇到的情况的问题。
设置:在 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);
}
}
}
在 impl.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 上编译得很好,调用了 impl.cpp 中定义的函数。当我使用其本机编译器在 HP-UX 上编译时,它似乎将调用解析为 stringStuff.h 中定义的调用,并抱怨将临时引用转换为非常量引用(这令人惊讶的是警告,而不是错误) ,以及尝试将 void 分配给 myString。
根据 C++ 标准应该发生什么,以及哪个编译器(如果有的话)是正确的?
顺便说一句,在我的情况下,我通过在对 trim 的调用前加上 :: 来“修复”它,尽管这并不理想。