I'm writing an app in C++ that interfaces with some code written in C.
The following is a simplified version of a struct defined in C code that I can't modify.
struct event{
uint8_t type;
union {
struct /* WishThisHadAName */ {
// ...
} connect;
struct {
// ...
} disconnect;
};
};
I'm trying to be able to define functions that take a pointer to the different unnamed structs, connect, disconnect, and all the others not listed above. To look something like the following.
void onConnect( struct WishThisHadAName *evt );
Is it possible to create this prototype without modifying the original C struct? Is there an easy way to create a wrapper for the unnamed structs to give them a name? Am I forced to create named structs that mirror the unnamed ones I'm trying to use?
I know you can get the type of an instantiated variable with decltype
but I can't figure out how I could use that or declval
in order to create the function prototype I'm looking for.
Simple as using event_connect_t = decltype(event::connect);
.
Then you can use it as void onConnect( event_connect_t *evt );
.
You can't declare a compatible type, but you can just extract the existing type declaration from the definition. decltype
can resolve static member references just fine.