D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2168 - cast extern enhancement?
Summary: cast extern enhancement?
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-06-24 10:41 UTC by david
Modified: 2024-12-13 17:48 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description david 2008-06-24 10:41:18 UTC
I'm not sure where this nntp://news.digitalmars.com/digitalmars.D.bugs:2556 links.

But seems this test case gets lost?

http://dstress.kuehne.cn/dstress/trunk/compile/extern_07.d

module dstress.compile.extern_07;

extern(C) void *wglGetProcAddress(char*);
extern(C) int function() glFunctionFoo;

int main(){
        glFunctionFoo = cast(extern(C) int function()) wglGetProcAddress("glFunctionFoo");
        return 0;
}
Comment 1 Stewart Gordon 2008-11-24 07:37:09 UTC
What exactly is the bug you're reporting?

- that that DStress test case seems to have been lost?  If so, the product is wrongly set.

- that the code behaves incorrectly?  If so, please be specific.
Comment 2 Don 2010-01-19 02:47:17 UTC
This was an enhancement request, to allow extern(C) inside cast().
Comment 3 RazvanN 2024-07-02 14:20:15 UTC
You can just declare the return type of wglGetProcAddress to be of type `int function()`:

extern(C) int function() func(char*);

And then it will work without needing the cast.
Comment 4 Dennis 2024-07-02 15:11:55 UTC
(In reply to RazvanN from comment #3)
> And then it will work without needing the cast.

That works in this reduced example, but when doing dynamic loading, you often load multiple functions with different signatures.
Comment 5 RazvanN 2024-07-03 07:16:18 UTC
(In reply to Dennis from comment #4)
> (In reply to RazvanN from comment #3)
> > And then it will work without needing the cast.
> 
> That works in this reduced example, but when doing dynamic loading, you
> often load multiple functions with different signatures.

In that case, this seems to work:

```
extern(C) void* func(char*);

void main()
{
    char* p = cast(char*)"func".ptr;
    extern(C) int function() glFunctionFoo = cast(int function())func(p);
    extern(C) int function(int) glFunctionFoo2 = cast(int function(int))func(p);
    pragma(msg, typeof(glFunctionFoo));
    pragma(msg, typeof(glFunctionFoo2));                                                                                                                                                                                                         
}
```

So, forcing the lhs of the assignment to extern(C) enables the cast to work.
Comment 6 dlangBugzillaToGithub 2024-12-13 17:48:33 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/17716

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB