3

下面的简单示例代码,带有生成的汇编程序。我很惊讶生成的代码涉及变体。Delphi 等价物当然不会。

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
4

1 回答 1

5

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

Variant __fastcall operator *(int lhs, const Variant& rhs)
{
    return Variant(lhs).operator *(rhs);
}

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

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

或者

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

或者

int x =  2 * (t2.Val - t1.Val); // best, minimum assembly is generated
于 2013-11-14T15:03:01.147 回答