2013-11-14 5 views
3

아래의 간단한 샘플 코드와 생성 된 어셈블러. 생성 된 코드에 변형이 포함되어 있다는 사실에 놀랐습니다. 델파이와 동등한 것은 아닙니다.TDateTime 계산에 변형이 포함되는 이유는 무엇입니까?

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

생성 된 코드. T2의 결과 것을

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 

답변

5

주 - T1은 TDateTime으로이고 어떤 운전자 상황이 컴파일러, TDateTime으로는 int 곱으로 정의되어 있지 않은 캐스트/불필요한 전환을 적용한다. 피연산자 모두 변형에 주조 된이 글로벌 범위 연산자 (왼쪽 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 
,
관련 문제