Problem with varargs directive

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with varargs directive

silvioprog
Hello,

The problem seems more related to Delphi, but I want to understand why it compiles only in FPC.

The declaration is just:

=== begin code ===
var
  FmtStr: function(const fmt: PAnsiChar): PAnsiChar; cdecl; varargs;
=== end code ===

And the full console project to test it in attachment.

It compiles only in FPC, in Delphi I got:

E2029 ',' or ':' expected but ';' found

Why? I don't know if it's a bug in Delphi or a breach in FPC.

ps. it compiles fine in FPC even using `$mode delphi`.

--
Silvio Clécio

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

vrs.zip (10K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Problem with varargs directive

Anthony Walter-3
I believe in Delphi only external functions may use varargs. In your case you are declaring a variable, which could be assigned to a local function.

function FmtStr(const fmt: PAnsiChar): PAnsiChar; varargs; cdecl; external yourlibname;

Note: varargs comes before cdecl, though I am not sure if it matters.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Problem with varargs directive

silvioprog
On Mon, Jan 11, 2016 at 3:41 PM, Anthony Walter <[hidden email]> wrote:
I believe in Delphi only external functions may use varargs. In your case you are declaring a variable, which could be assigned to a local function.

You are right:



So it seems that I can't use `varargs` loading libraries dynamically on Delphi. :-(

function FmtStr(const fmt: PAnsiChar): PAnsiChar; varargs; cdecl; external yourlibname;
 
I need to use it in variables because I wanna load my library dynamically. :-/

Note: varargs comes before cdecl, though I am not sure if it matters.

It seems irrelevant, but I saw it in some Delphi units declaring it as `... cdecl; varargs; ...`.

--
Silvio Clécio

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Problem with varargs directive

silvioprog
On Mon, Jan 11, 2016 at 3:56 PM, silvioprog <[hidden email]> wrote:
On Mon, Jan 11, 2016 at 3:41 PM, Anthony Walter <[hidden email]> wrote:
I believe in Delphi only external functions may use varargs. In your case you are declaring a variable, which could be assigned to a local function.

You are right:
[...]

Fixed. I just removed the first `;`:

=== begin code ===
var
  FmtStr: function(const fmt: PAnsiChar): PAnsiChar; cdecl varargs;
=== end code ===

Now it compiles fine in an original copy of Delphi Seattle and in the FPC from trunk (3.1.1).

I'm glad to compile that, but it is very strange, according EMB docs:

"The varargs directive works only with external routines and only with the cdecl calling convention.":


But I saw many programmers using it in funcs declared as variable:


So I'm going to use it too hehe! :-)

--
Silvio Clécio

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Problem with varargs directive

silvioprog
On Mon, Jan 11, 2016 at 4:18 PM, silvioprog <[hidden email]> wrote:
[...]
So I'm going to use it too hehe! :-)

Worked: http://pastebin.com/DjSkq7U8. :-)

--
Silvio Clécio

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal