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.

doptimusprime
  • 9,115
  • 6
  • 52
  • 90
  • 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++. – user657267 Jul 25 '14 at 04:43
  • 1
    Looks like an [old bug](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=29365), what version of GCC are you using? – user657267 Jul 25 '14 at 04:48
  • @user657267: Sorry for the inconvenience, see the edit. This code must reproduce the warning. – doptimusprime Jul 25 '14 at 04:55
  • No warnings here, even with `-Wall -Wextra -Wpedantic`, again what version of GCC are you using? Run `g++ -v`. – user657267 Jul 25 '14 at 04: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. –  Jul 25 '14 at 05:02
  • @hvd I didn't see the edit. – user657267 Jul 25 '14 at 05:11
  • @user657267 Okay. Your comment was in response to the OP's comment "see the edit". Do check for edits after such a comment. :) –  Jul 25 '14 at 05:12

1 Answers1

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.