以下是来自JwaStrSafe.pas(来自Jedi Api)的代码的相关部分,我正在使用UNICODE定义的符号进行编译:
@H_403_2@type STRSAFE_LPWSTR = PWIDECHAR; PSTRSAFE_LPWSTR = ^STRSAFE_LPWSTR; {$IFDEF UNICODE} STRSAFE_LPTSTR = STRSAFE_LPWSTR; PSTRSAFE_LPTSTR = ^STRSAFE_LPTSTR; {$ELSE} ... {$ENDIF} ... //function declaration function StringCchCopyExW( {__out_ecount(cchDest)}pszDest : STRSAFE_LPWSTR; {__in}cchDest : size_t; {__in}const pszSrc : STRSAFE_LPCWSTR; {__deref_opt_out_ecount(pcchRemaining^)}ppszDestEnd : PSTRSAFE_LPWSTR; {__out_opt}pcchRemaining : PSize_t; {__in}dwFlags : Cardinal) : HRESULT; stdcall; forward; external; ... //var passed to function ppszDestEnd : PSTRSAFE_LPTSTR; ... {$IFDEF UNICODE} result := StringCchCopyExW(pszDest,cchDest,pszSrc,ppszDestEnd,pcchRemaining,dwFlags); {$ELSE} result := StringCchCopyExA(pszDest,dwFlags); {$ENDIF}我在参数ppszDestEnd上调用StringCchCopyExW时出错.
查看类型定义我明白PSTRSAFE_LPTSTR是STRSAFE_LPTSTR的指针类型,它只是STRSAFE_LPWSTR的别名,为什么PSTRSAFE_LPTSTR和PSTRSAFE_LPWSTR不兼容?
解
感谢大卫的解释我取代了
同
@H_403_2@PSTRSAFE_LPTSTR = PSTRSAFE_LPWSTR;谢谢
解决方法
这也产生了E2010.但是,如果启用type-checked pointers选项,则代码将成功编译.实际上,该编译器选项的文档指出:
In the {$T-} state,distinct pointer types other than Pointer are incompatible (even if they are pointers to the same type). In the {$T+} state,pointers to the same type are compatible.
感谢Ken White为我指出了有用的帮助主题Type Compatibility and Identity.相关的摘录是T1和T2类型在以下情况下是分配兼容的:
T1 and T2 are compatible pointer types.
文档还指出类型是类型兼容的,如果:
Both types are (typed) pointers to the same type and the {$T+} compiler directive is in effect.
所以这记录了观察到的行为并引导我到这个例子:
@H_403_2@program PointerTypeCompatibilityTake2; {$APPTYPE CONSOLE} {$TYPEDADDRESS OFF} var P1,P2: ^Integer; P3: ^Integer; begin P1 := P2;//compiles P1 := P3;//E2008 Incompatible types end.所以,总结一下:
>当禁用类型检查指针时,如果指针属于同一类型,则指针是赋值兼容的.
>当启用类型检查指针时,如果指针指向相同类型,则指针是赋值兼容的.
我不得不承认对类型检查指针设置背后的历史和推理一无所知,所以我无法解释为什么编译器就是这样的.