Forum > Beginners

[Solved] Is it possible to const an argument passed by reference in FPC?

(1/13) > >>

ikel:
Hi,

In C++ we can const an argument passed by reference,


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---bool isValidEntry(const std::string &input)
Is it possible to do this in FPC?

Thanks.

440bx:
Yes, in FPC instead of "const" you specify "constref".

marcov:
By reference parameters in Pascal don't base on pointer syntax like in C (& or *) .  You therefore don't need to micro manage it, but use proper by ref language constructs like const/var/out.

constref only makes a const argument forcedly by ref, but this is rarely used because strings and dynamic arrays already contain an implicit ref. So the most direct translation would be


--- Code: ---function isValidEntry(const input: string):boolean;

--- End code ---

Besides this there is also the formal and open array parameter which also implies ref, and can be used for e.g. static arrays.

procedure x(const y);   // untyped (const void pointer)
procedure x(const y:array of z);

So a syntax where you can assign the pointer of a by ref argument doesn't exist with const or var. The whole problem simply doesn't exist, because for common cases there are proper language constructs to select. C is a blunt axe, and only has pointer syntax, hence the problems and weird mitigations like &

440bx:

--- Quote from: marcov on July 24, 2019, 10:38:47 am ---strings and dynamic arrays already contain an implicit ref.

--- End quote ---
For strings, that is only true when LONGSTRINGS ON is in effect and the parameter is not a shortstring. Otherwise, constref is required to declare the string is passed by reference (instead of value) and that it cannot be modified.

ASerge:

--- Quote from: 440bx on July 24, 2019, 10:51:45 am ---For strings, that is only true when LONGSTRINGS ON is in effect and the parameter is not a shortstring. Otherwise, constref is required to declare the string is passed by reference (instead of value) and that it cannot be modified.

--- End quote ---

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$APPTYPE CONSOLE}{$MODE OBJFPC} procedure Test(const S: ShortString);begin  Writeln(S);end; begin  Test('1');end.FPC 3.0.4 x64

--- Code: ASM  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---.section .text.n_p$program_$$_test$shortstring,"x"        .balign 16,0x90.globl  P$PROGRAM_$$_TEST$SHORTSTRINGP$PROGRAM_$$_TEST$SHORTSTRING:.Lc1:# Temps allocated between rbp-16 and rbp-8.seh_proc P$PROGRAM_$$_TEST$SHORTSTRING.Ll1:# [project1.lpr]# [5] begin        pushq   %rbp.seh_pushreg %rbp.Lc3:.Lc4:        movq    %rsp,%rbp.Lc5:        leaq    -48(%rsp),%rsp.seh_stackalloc 48        movq    %rbx,-16(%rbp).seh_savereg %rbx, 32.seh_endprologue# Var S located at rbp-8, size=OS_64        movq    %rcx,-8(%rbp).Ll2:# [6] Writeln(S);        call    fpc_get_output...# [10] Test('1');        leaq    _$PROGRAM$_Ld1(%rip),%rax        movq    %rax,%rcx        call    P$PROGRAM_$$_TEST$SHORTSTRING.Ll7:# [11] end.Same thing in x32. A short string is passed by reference. There is no allocation and copying inside the function.

Navigation

[0] Message Index

[#] Next page

Go to full version