【发布时间】:2012-03-18 12:24:30
【问题描述】:
我想知道是否有人知道为什么以下两段代码的行为非常不同。我可以理解为什么第二个不起作用,但为什么第一个起作用?在同一位置int x = gc.f(); 模板应该被实例化,所以会发生同样的错误,但为什么实际上没有错误?
a.cpp
#include <iostream>
using namespace std;
template <typename T>
struct A {
struct B {
};
};
template <typename T>
struct C {
typedef A<C<T> > D;
int f() {
typename D::B p;
return 0;
}
};
C<float> gc;
int x = gc.f();
template <typename T>
struct A<C<T> > {
struct B {
B() {
cout << "B::B()" << endl;
}
~B() {
cout << "B::~B()" << endl;
}
};
};
int main() {
}
输出
B::B()
B::~B()
和
a2.cpp
#include <iostream>
using namespace std;
template <typename T>
struct A {
struct B {
};
};
struct C {
typedef A<C> D;
int f() {
D::B p;
return 0;
}
};
C gc;
int x = gc.f();
template <>
struct A<C> {
struct B {
B() {
cout << "B::B()" << endl;
}
~B() {
cout << "B::~B()" << endl;
}
};
};
int main() {
}
编译器错误
a2.cpp:24: error: specialization of ‘A<C>’ after instantiation
a2.cpp:24: error: redefinition of ‘struct A<C>’
a2.cpp:6: error: previous definition of ‘struct A<C>’
【问题讨论】:
-
时间和它有什么关系?
-
对不起,我实际上是想问关于实例化后'A
'的专业化的问题
标签: c++