【发布时间】:2011-04-27 01:28:26
【问题描述】:
这段代码片段:
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>());
}
在 Comeau 上给出以下错误,在 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 之外声明,则它会编译。
对此有何解释?
【问题讨论】:
标签: c++ templates namespaces compiler-errors overload-resolution