澄清C 名称查找
Clarification on C++ name lookup
我对刚刚上班的情况有一个疑问。
设置:在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编译器是错误的。
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 在C++中查找文件
- 模板元程序查找相似的连续类型名称
- 在UNIX系统中使用DIR查找文件的字节大小
- 查找最接近的大于当前数字的数字的索引
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 查找后更改类变量
- 使用正则表达式regex_search在字符串中查找字符串
- 使用gcc从静态链接的文件中查找可选符号
- 在C++中查找范围的长度
- 算法问题:查找从堆栈中弹出的所有序列
- 在Windows中查找扬声器输出的当前音量级别
- 如何在C++中使用X509证书模在令牌中查找私钥
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在 for 循环中查找问题时遇到困难
- 如何在文件中查找字节序列
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 澄清C 名称查找
- 帮助,澄清我对不合格名称查找的疑问:ISO n3290草案