具有类参数的函数从命名空间泄漏? [英] Functions with class arguments are leaked from a namespace?
问题描述
我有一小块代码在这里供您考虑,这让我很困惑。奇怪的是,它编译在Sun Studio和GCC上,即使我认为它不应该。
请考虑这:
命名空间名称
{
class C
{
int a;
};
void f(C c);
void g(int a);
}
int main(int argc,char ** argv)
{
name :: C c;
name :: f(c);
f(c); //< --- this compiles,strangely enough够
name :: g(42);
// g(42); <--- this does not,as I expected
}
相同的命名空间导致函数 f
从命名空间中泄漏,并且可以不使用 name ::
访问。 / p>
有没有人有这样的解释?
它被称为测量依赖查找(或Koenig查找)。总之,编译器将在命名空间中寻找作为参数类型的命名空间的函数。
I have a small piece of code here for your consideration which puzzles me quite a lot. The strange thing is that it compiles on both Sun Studio and GCC even though I think it should not.
Consider this:
namespace name
{
class C
{
int a;
};
void f(C c);
void g(int a);
}
int main(int argc, char** argv)
{
name::C c;
name::f(c);
f(c); // <--- this compiles, strangely enough
name::g(42);
// g(42); <--- this does not, as I expected
}
The class argument from the same namespace causes the function f
to 'leak' out of the namespace and be accessible without name::
.
Does anybody have an explanation for this? It is certainly me and not the compiler being wrong here.
It's called argument-dependent lookup (or Koenig lookup). In short, the compiler will look for the function in namespaces that are the namespaces of argument types.
这篇关于具有类参数的函数从命名空间泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!