5

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.

7
  • 2
    I get no errors on GCC 4.9, it might be a bug in whatever version you're using unless the error is related to the context in which you're using it. Also _S1 is a reserved identifier, and you don't need to typedef struct in c++. Commented Jul 25, 2014 at 4:43
  • 1
    Looks like an old bug, what version of GCC are you using? Commented Jul 25, 2014 at 4:48
  • @user657267: Sorry for the inconvenience, see the edit. This code must reproduce the warning. Commented Jul 25, 2014 at 4:55
  • No warnings here, even with -Wall -Wextra -Wpedantic, again what version of GCC are you using? Run g++ -v. Commented Jul 25, 2014 at 4:57
  • @user657267 I do see the warning, with the exact code in the question now, with GCC versions 4.5.4, 4.6.4, 4.7.4, 4.8.3, 4.9.1, no special warning flags needed. If you don't, I suspect either your GCC is really old, or you're missing something important in your test. Commented Jul 25, 2014 at 5:02

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.