C++builder 为什么TDateTime计算涉及变量?

C++builder 为什么TDateTime计算涉及变量?,c++builder,variant,tdatetime,C++builder,Variant,Tdatetime,下面是简单的示例代码,带有生成的汇编程序。我很惊讶生成的代码包含变体。德尔福的等价物当然不是 TDateTime t1; TDateTime t2; ... int x = 2 * (t2 - t1); 生成的代码 Unit23.cpp.18: int x = 2 * (t2 - t1); 00401814 66C745C82400 mov word ptr [ebp-$38],$0024 0040181A 8D45DC lea eax,[ebp-$24] 00

下面是简单的示例代码,带有生成的汇编程序。我很惊讶生成的代码包含变体。德尔福的等价物当然不是

TDateTime t1;
TDateTime t2;
...
int x =  2 * (t2 - t1);
生成的代码

Unit23.cpp.18: int x =  2 * (t2 - t1);
00401814 66C745C82400     mov word ptr [ebp-$38],$0024
0040181A 8D45DC           lea eax,[ebp-$24]
0040181D E852180000       call $00403074
00401822 50               push eax
00401823 FF45D4           inc dword ptr [ebp-$2c]
00401826 8D55A8           lea edx,[ebp-$58]
00401829 8D45A0           lea eax,[ebp-$60]
0040182C E8FB000000       call System::TDateTime::operator -(const System::TDateTime &)
00401831 DD5D94           fstp qword ptr [ebp-$6c]
00401834 8D5594           lea edx,[ebp-$6c]
00401837 8D45EC           lea eax,[ebp-$14]
0040183A E8F1180000       call $00403130
0040183F FF45D4           inc dword ptr [ebp-$2c]
00401842 8D55EC           lea edx,[ebp-$14]
00401845 B802000000       mov eax,$00000002
0040184A 59               pop ecx
***
0040184B E808010000       call System::operator *(int,const System::Variant &)
***
00401850 8D45DC           lea eax,[ebp-$24]
00401853 E8001A0000       call $00403258
00401858 89459C           mov [ebp-$64],eax
0040185B FF4DD4           dec dword ptr [ebp-$2c]
0040185E 8D45DC           lea eax,[ebp-$24]
00401861 BA02000000       mov edx,$00000002
00401866 E811190000       call $0040317c
0040186B FF4DD4           dec dword ptr [ebp-$2c]
0040186E 8D45EC           lea eax,[ebp-$14]
00401871 BA02000000       mov edx,$00000002
00401876 E801190000       call $0040317c
0040187B 66C745C81800     mov word ptr [ebp-$38],$0018

请注意,t2-t1的结果是TDateTime,并且没有定义任何运算符将int与TDateTime相乘,在这种情况下,编译器将应用不需要的转换/强制转换。两个操作数都已强制转换为Variant,并调用此全局范围运算符(左侧为int):

我建议您通过指定操作数类型来防止不必要的强制转换,因此应将表达式更改为:

int x =  2 * (t2 - t1).Val;

int x =  2 * (t2 - t1).Val;
int x =  2 * (int)(t2 - t1);
int x =  2 * (t2.Val - t1.Val); // best, minimum assembly is generated