Thread overview
type in extern declaration
Sep 20, 2021
NonNull
Sep 20, 2021
Paul Backus
Sep 21, 2021
Tejas
September 20, 2021

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.

September 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:

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:

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.

September 21, 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/