定义BREAK时,g ++ 4.7.2不会编译以下内容,我认为这是有效的C++。如果A<U> tmp
更改为其他内容(例如A<int> tmp
),它会在BREAK定义的情况下进行编译 - 但这使得此处的最小测试用例可以正常工作,但在我的实际应用程序中并不好。这里有什么不合法的C++吗?g ++不编译某些嵌套模板
template <typename T>
class B {
};
template <typename T>
class A {
public:
template <typename U> B<U> *alloc_B();
};
template <typename T> template <typename U>
B<U> *A<T>::alloc_B() {
return new B<U>();
}
#ifdef BREAK
template <typename T>
class C {
public:
template <typename U> void x(B<U> &b) {
A<U> tmp;
B<U> *tmp2;
tmp2 = tmp.alloc_B<U>();
delete tmp2;
}
};
#endif
int main() {
A<int> a;
B<float> *bp = a.alloc_B<float>();
delete bp;
#ifdef BREAK
C<int> c;
B<float> b;
c.x(b);
#endif
}