digitalmars.D.learn - type in extern declaration
- NonNull (15/15) Sep 20 2021 How do I write the type of a function so as to alias it, and use
- Paul Backus (14/20) Sep 20 2021 You can create an alias to the type:
- Tejas (3/18) Sep 20 2021 This is for C++, but I think you'll find it helpful all the same
How do I write the type of a function so as to alias it, and use that in an extern(C) declaration? For example, how do I write the type of an external function int main2(int argc, char** argv) { // } ? This is not int function(int, char**) because this is the type of a function pointer of the right type, not the type of the function itself. If I had that type aliased to mainfunc I could write extern(C) mainfunc main2; in D source to arrange to call it, and similarly any other functions of the same type.
Sep 20 2021
On Monday, 20 September 2021 at 20:02:24 UTC, NonNull wrote:How do I write the type of a function so as to alias it, and use that in an extern(C) declaration? For example, how do I write the type of an external function int main2(int argc, char** argv) { // }You can create an alias to the type: ```d alias MainFunc = int(int, char**); ``` However, you can't use that alias to declare a variable, because variables are not allowed to have function types: ```d MainFunc main; // Error: variable `main` cannot be declared to be a function ``` The only way you can declare a function in D is with a [function declaration.][1] [1]: https://dlang.org/spec/function.html#FuncDeclaration
Sep 20 2021
On Monday, 20 September 2021 at 20:02:24 UTC, NonNull wrote:How do I write the type of a function so as to alias it, and use that in an extern(C) declaration? For example, how do I write the type of an external function int main2(int argc, char** argv) { // } ? This is not int function(int, char**) because this is the type of a function pointer of the right type, not the type of the function itself. If I had that type aliased to mainfunc I could write extern(C) mainfunc main2; in D source to arrange to call it, and similarly any other functions of the same type.This is for C++, but I think you'll find it helpful all the same https://atilaoncode.blog/2019/03/07/the-joys-of-translating-cs-stdfunction-to-d/
Sep 20 2021