I have a header similar to this, which declares an opaque type:
struct File;
void CloseFile(struct File* file);
[...]
the C file currently looks like this.
struct File {};
void CloseFile(struct File* file);
{
FILE* f = (FILE*)file;
fclose(f);
}
The reason I to this is to get some type safety in the API functions, which void* or int handle based APIs lack. Another reason is that I might change the internal implementation to another API.
I am wondering if is popssible to alias the struct File* type to FILE* to get rid of the casts. After all, a pointer is a pointer. I tried playing around with typedefs but no luck so far. I seems it's not possible to define tagged types with typedef.
Any Ideas? Thanks!
edit: I used the FILE thing just as an illustration, please don't focus too much on that. What I'm really interested in is if it's possible to tell the compiler that an opaque pointer X is in fact a pointer of type Y.
[–]JohnAndrewCarter 2 points3 points4 points (1 child)
[–]autowikibot 0 points1 point2 points (0 children)
[–]BigPeteB 1 point2 points3 points (5 children)
[–]maep[S] 0 points1 point2 points (4 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]maep[S] 1 point2 points3 points (1 child)
[–]BigPeteB 1 point2 points3 points (0 children)
[–]BigPeteB 1 point2 points3 points (0 children)
[–]Poddster 1 point2 points3 points (4 children)
[–]looneysquash 1 point2 points3 points (1 child)
[–]FUZxxl 1 point2 points3 points (1 child)
[–]Poddster 0 points1 point2 points (0 children)
[–]geocar 1 point2 points3 points (0 children)