简体   繁体   English

为什么通用类型中不允许使用T类?

[英]Why is class of T not allowed in a generic type?

Consider this program: 考虑这个程序:

type
  TMyClass<T: class> = class
  public type
    TClassOfT = class of T;
  end;

begin
end.

This fails to compile at the declaration of TClassOfT with the error message: 这无法在TClassOfT的声明中编译,并显示错误消息:

E2021 Class type required

Can anyone explain why this is so? 任何人都可以解释为什么会这样吗? If I were to instantiate this class with, for instance TMyClass<TObject> then surely class of TObject would be meaningful. 如果我要使用例如TMyClass<TObject>来实例化这个类,那么肯定class of TObject是有意义的。 I constrained the generic parameter to be a class. 我将泛型参数约束为一个类。 So how can the compiler object that T is not a class type? 那么编译器如何反对T不是类类型呢?

So far as I can tell, there is no reason for the compiler to reject this code. 据我所知,编译器没有理由拒绝此代码。 Thus, I believe that it is a design flaw and have submitted QC#121178 . 因此,我认为这是一个设计缺陷,并提交了QC#121178

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM