Forward declaration of anonymous structure from C to C ++

I have a third-party C library with an anonymous structure similar to

typedef struct { ... }A; 

This file is automatically created through the program, so it can change based on something

Now in a C ++ project, I accept an argument of the above type, for example, like

  void foo(const A & in) { ... } 

How to redirect declaration A to a header file defining the above function?

I tried:

  typedef struct AA; 

or

  struct A; 

as well as

  typedef AA; 

out of curiosity.

However, this leads to a compile-time error when overriding a type. Thanks

+6
source share
2 answers

Unfortunately, you are out of luck.

A is a typedef for an unlabeled struct . And you cannot forward the declaration, since you cannot directly refer to this struct .

Can you change the generator to output typedef struct A { ... } A; ? Then it will compile well in both C and C ++, and you can redirect the declaration to C ++.

Otherwise, you have two options: 1. #include file containing a struct declaration. 2. Copy the struct manually. Obviously, (1) is much preferable.

+6
source

You cannot, because type aliases cannot be declared forward. Only actual types can.

+1
source

All Articles