Why is the following code is not correct?
template <typename T>
class B {
public:
auto foo() -> decltype(static_cast<T*>(this)->foo()) {
return static_cast<T*>(this)->foo();
}
};
template <typename X>
class A: public B<A<X>> {
public:
X foo();
};
template <>
class A<int>: public B<A<int>> {
public:
int foo();
};
Compiler gives the following error (highlighting decltype(static_cast<T*>(this)->foo())
part):
static_cast from 'B<A<int>> *' to 'A<int> *', which are not related by inheritance, is not allowed
in instantiation of template class B<A<int>> requested here
'A' is incomplete
'B' is incomplete
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…