0

I am working on a third-party module wrapper. I hope my main header file does not have any third-party related header files. Every parameter type and return type are opaque by only declaring it. But there is a anonymous structure defined like the following:

typedef struct {
    int x;
    int y;
    int width;
    int height;
} IppiPoint;

I cannot modify the third-party header file. I have no idea to declare it. Here are what I tried and error messages I got

1.

struct IppiPoint;
error C2371: 'IppiPoint' : redefinition; different basic types

2.

typedef struct IppiPoint;
... warning C4091: 'typedef ' : ignored on left of 'IppiPoint' when no variable is declared
... error C2371: 'IppiPoint' : redefinition; different basic types

How do I declare such anonymous struct?

Chen OT
  • 3,486
  • 2
  • 24
  • 46
  • Why do you want to declare the structure? You are clearly not supposed to do that, only use the type-alias already provided by your third-part header file. – Some programmer dude Mar 30 '16 at 11:36
  • I want to move the type-alias to cpp file, then my client will not see any third-party header when they use. – Chen OT Mar 30 '16 at 11:38
  • What about `IppiPoint;`? – Fynn Mar 30 '16 at 11:42
  • @Fynn Do you mean declare it without struct? It makes compiler complain missing type specifier. – Chen OT Mar 30 '16 at 11:44
  • Well currently your source *do* "see" the third-part header file (otherwise you would not get the "redefinition" error). And if you declare non-pointer variables of this type, or try to allocate using `malloc`, then you *need* the full structure anyway. – Some programmer dude Mar 30 '16 at 11:55
  • You can't forward-declare an anonymous struct. The anonymous struct is a terrible form of keystroke-saving. You need to either modify the header or wrap the struct in a type of your own. – molbdnilo Mar 30 '16 at 11:57
  • @JoachimPileborg Actually I does not wanna my client directly use the ippiPoint. I have a custom struct `MyPoint` as middle layer. But I wanna `MyPoint` more convenient, I put a `operator ippiPoint () const;` in it. And this is the only place needed to see ippiPoint. So I am searching possible ways to only declare it and hide the real structure definition in cpp file. – Chen OT Mar 30 '16 at 12:02
  • So you have a conversion operator that converts instance of `MyPoint` to the `IppiPoint` type? And then you say that's the only use of the `IppiPoint` type? That makes no sense, you create a conversion operator that you are not going to use? If and when and where you use the conversion operator you also use the type, and need the full structure, there's no way to circumvent that. – Some programmer dude Mar 30 '16 at 12:09
  • My thought is All clients use `MyPoint` as parameter to my functions (include header). And I also use `MyPoint` in cpp to pass (implicitly convert) IppiPoint to imp function. Yeah you are right. It may be not well to put these things all in `MyPoint`. If I wanna hide all the details, I should define such conversion function in cpp, too. Thanks – Chen OT Mar 30 '16 at 12:14

1 Answers1

1

Create your own type struct Bob;. Use it instead.

In the cpp, define struct Bob as follows:

struct Bob {
  IppiPoint contents;
};

and so long as IppiPoint is standard-layout, you can reinterpret_cast<IppiPoint*>(pointer_to_bob) legally.

A pointer to an object of standard-layout struct type can be reinterpret_cast to pointer to its first non-static data member (if it has non-static data members) or otherwise its first base class subobject (if it has any), and vice versa. (padding is not allowed before the first data member). Note that strict aliasing rules still apply to the result of such cast.

Maybe pick a better name than Bob.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524