简体   繁体   English

通用类型的大小

[英]Size of generic type

Is there any way to determine the size in bytes of something like 有没有办法确定类似的字节大小

TItem <T> = record
  Data : T;
end;

Can I write something like 我能写点什么吗?

function TItem <T>.GetByteSize : Integer;
begin
if (T = String) then
  Result := GetStringByteSize (Data as String)
else
  Result := SizeOf (Data);
end;

or perhaps with the help of specialization? 或者也许是在专业化的帮助下?

function TItem <String>.GetByteSize : Integer;
begin
  Result := GetStringByteSize (Data)
end;

function TItem <T>.GetByteSize : Integer;
begin
  Result := SizeOf (Data);
end;

Thanks! 谢谢!

Is there something wrong with taking the size of the instantiated type? 获取实例化类型的大小是否有问题?

SizeOf(TItem<string>)

Or you could define GetByteSize like this: 或者您可以像这样定义GetByteSize:

function TItem <T>.GetByteSize : Integer;
begin
  Result := SizeOf(TItem<T>);
end;

不,据我所知,你不能专注于类型

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

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