什么是关于什么参数依赖查找的好解释?很多人也称它为Koenig Lookup.
我最好知道:
为什么我必须写,std::cout而不是std::<<像这样的代码行:
#include <iostream>
int main() {
std::cout << "Hello, world!";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
cout来自std库,<<通常不习惯进行位移?那么,为什么我::之前也不必编写范围运算符<<,因为它也用于其他含义?编译器如何知道后std::cout,<<意味着另一件事?
我用g ++ 4.6和4.8编译了这段代码的错误.g ++ 4.2和4.4就可以了.这是一个bug还是一些新的语言功能?
template <typename T>
struct A { typedef typename T::value_type type; };
template <typename U>
struct B
{
void bar () { }
void foo ()
{
// OK
this->bar ();
// OK
(*this).bar ();
// Error in g++ 4.6-4.8
// leads to full instantiating of template arg "U"
(&*this)->bar ();
}
};
int main ()
{
B< A<void> > b;
b.foo ();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g ++ inst.cc
inst.cc: In instantiation of ‘struct A<void>’:
inst.cc:20:5: …Run Code Online (Sandbox Code Playgroud) 这看起来有点落后于我,但它有效:
#include <iostream>
namespace nTest
{
struct cTest {};
void fTest(cTest& x)
{
std::cout << "nTest::fTest(cTest&) called" << std::endl;
}
}
int main(void)
{
nTest::cTest x;
fTest(x); //Weird! fTest is resolved since its parameter belongs to nTest.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
通常,您需要nTest ::才能访问fTest,但其属于nTest的参数似乎将nTest添加到搜索fTest的可能范围列表中.我觉得参数范围影响函数查找似乎很奇怪.
这在GCC编译得很好,但我想知道这种用法是否可移植?这个范围机制的官方定义是什么?
c++ ×4
c++-faq ×1
gcc ×1
name-lookup ×1
namespaces ×1
parameters ×1
portability ×1
scope ×1
templates ×1