digitalmars.D.learn - C's void func() vs. void func(void).
- ciechowoj (12/12) Jul 29 2016 In C, a function `void func()` doesn't declare a function without
- Mike Parker (9/21) Jul 29 2016 Yes, this is correct as long as the calling convention is not
- Mike Parker (10/12) Jul 29 2016 Though, I should add the caveat that you need to ensure the
- ciechowoj (2/11) Jul 29 2016 Thanks, good to know.
- ag0aep6g (2/5) Jul 29 2016 extern(System), no?
- Mike Parker (2/9) Jul 29 2016 Yeah, that's what I had intended.
In C, a function `void func()` doesn't declare a function without arguments, instead it declares a function that takes unspecified number of arguments. The correct way to declare a function that takes no arguments is to use the `void` keyword: `void func(void)`. What is the correct way to refer to such a function (`void func()`) from D bindings? If I assume that the unspecified number of arguments (for some particular function) is equal to zero, is `extern (C) void func()` a correct D binding to the both functions `void func()` and `void func(void)` declared in C? Specifically, I'm concerned about calling convention issues.
Jul 29 2016
On Friday, 29 July 2016 at 10:57:37 UTC, ciechowoj wrote:In C, a function `void func()` doesn't declare a function without arguments, instead it declares a function that takes unspecified number of arguments. The correct way to declare a function that takes no arguments is to use the `void` keyword: `void func(void)`. What is the correct way to refer to such a function (`void func()`) from D bindings? If I assume that the unspecified number of arguments (for some particular function) is equal to zero, is `extern (C) void func()` a correct D binding to the both functions `void func()` and `void func(void)` declared in C? Specifically, I'm concerned about calling convention issues.Yes, this is correct as long as the calling convention is not stdcall or something else: extern(C) void func(); If you're dealing with stdcall: extern(Windows) void func(); And if it is a cross-platform library that is stdcall on Windows and cdecl elsewhere: extern(C) void fun();
Jul 29 2016
On Friday, 29 July 2016 at 12:15:22 UTC, Mike Parker wrote:Yes, this is correct as long as the calling convention is not stdcall or something else:Though, I should add the caveat that you need to ensure the definition of the C function does not specify any parameters. AFAIK, this is legal: // foo.h void func(); // foo.c void func(int a, int b) { ... } In which case you would want to include the parameters in your binding.
Jul 29 2016
On Friday, 29 July 2016 at 12:20:17 UTC, Mike Parker wrote:Though, I should add the caveat that you need to ensure the definition of the C function does not specify any parameters. AFAIK, this is legal: // foo.h void func(); // foo.c void func(int a, int b) { ... } In which case you would want to include the parameters in your binding.Thanks, good to know.
Jul 29 2016
On 07/29/2016 02:15 PM, Mike Parker wrote:And if it is a cross-platform library that is stdcall on Windows and cdecl elsewhere: extern(C) void fun();extern(System), no?
Jul 29 2016
On Friday, 29 July 2016 at 18:24:52 UTC, ag0aep6g wrote:On 07/29/2016 02:15 PM, Mike Parker wrote:Yeah, that's what I had intended.And if it is a cross-platform library that is stdcall on Windows and cdecl elsewhere: extern(C) void fun();extern(System), no?
Jul 29 2016