Delphi short-circuit compiler directive not working?

I am trying to migrate a project from Delphi 4 to Delphi XE2. I have a requirement to use shortstring in a project. According to Delphi's help, $ H- should force the compiler to use short lines for type string . I used this directive, but I do not see any difference. I wrote a small test program:

 program Stringtest; {$APPTYPE CONSOLE} {$R *.res} {$H- } uses System.SysUtils; var str : string; short : shortstring; begin try str := 'testing'; short := 'testing'; Writeln('str ' +Format('%d', [sizeOf(str)]) ); Writeln('short ' +Format('%d', [sizeOf(short)]) ); Readln; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. 

In my opinion, the output should be the same for str and short , because the compiler should be considered as shortstring . But size 4 for str and 256 for short . Is there any other way for the compiler to treat string as a shortstring or is it the only solution to replace all occurrences of string with shortstring in the source code?

+4
source share
1 answer

$ H- is deprecated, if only for backward compatibility. It is not possible to force Delphi XE2 (or any version of the Delphi compiler with D2009 and later) to not use Unicode as the default string type.

The compiler directive still exists only to prevent hacking of old code during compilation. There was some noise when Delphi 2009 was released with Unicode only as the default string type, without switching, to return to using AnsiString by default. It was explained that they determined that it would be almost impossible due to the need to have two different versions of RTL, VCL, etc. In order for the ShortString value to become standard, the same thing is required.

You will need to explicitly change all your references to string strings (or, even better, fix your code so as not to require them).

+7
source

Source: https://habr.com/ru/post/1399953/


All Articles