简体   繁体   中英

Warning Anonymous namespace inside struct using function pointer

In my program:

//Put this code in a separate header file.
struct _S1;
typedef struct {int unused;} * RETVAL;

typedef RETVAL (*MyFunc) (void* result, void* ctx, struct _P1* s);
typedef struct _S1 {
    struct _S1 *parent;
    MyFunc f1;
} S1;

//In cpp file, include the above header file.

I get the following warning:

warning: ‘_S1’ has a field ‘_S1::f1’ whose type uses the anonymous namespace [enabled by default]
 typedef struct _S1 {
            ^

What is the meaning of this warning? What is the result of this warning in my code? How to get rid of this warning?

I am compiling on gcc on Linux.

The fact that you put your type definitions in a header strongly suggests that you want multiple source files to use that header, and to use those types.

But if multiple source files include that header, they each get their own version of RETVAL , because of the anonymous struct you're using. Yet at the same time, _S1 would be the same type across all source files. That's not possible.

Traditional compilers don't care about this: they don't perform whole-program optimisations. More modern compilers do, and they need to be able to tell whether two type definitions are really the same type. In order for them to be able to tell, your code has to be very accurate.

The simplest solution is to give your anonymous struct a name. A named struct is the same type across all source files, and so is a pointer to a named struct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM