这段代码:奇怪的编译错误有关重载
namespace ns
{
struct last;
struct first
{
typedef last next;
};
template <typename T>
struct chain
{
chain<typename T::next> next;
};
template <>
struct chain<last>
{
};
}
using namespace ns;
template <typename T>
void f(const T& x) // #1
{
f(x.next);
}
void f(const chain<last>&) // #2
{
}
int main()
{
f(chain<first>());
}
给出科莫下面的错误,并在GCC一个非常相似的错误:
"ComeauTest.c", line 27: error: class "ns::chain<ns::last>" has no member "next"
f(x.next);
^
detected during:
instantiation of "void f(const T &) [with T=ns::chain<ns::last>]"
at line 27
instantiation of "void f(const T &) [with T=ns::chain<ns::first>]"
at line 36
它编译,但是,如果任#2
是在#1
之前定义的,或者last
是在ns
之外声明的。
对此有何解释?
但'#1'是'main',其中'#2'可见之后的POI。 – uj2 2010-10-18 17:18:19
@ uj2:'#2'在被调用的位置不可见。 – 2010-10-18 17:23:47
我的困惑(也许是uj2的)是,在main调用f选择模板化的f而不是非模板化的f。在'main'的调用中,这两个函数都是可见的,对吗?这是为什么发生? – JoshD 2010-10-18 17:37:54