简体   繁体   English

使用函数指针在struct中警告匿名命名空间

[英]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. 我在Linux上编译gcc。

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. 但是如果多个源文件包含该头文件,则每个文件都会获得自己的RETVAL版本,因为您使用的是匿名结构。 Yet at the same time, _S1 would be the same type across all source files. 但与此同时, _S1在所有源文件中的类型相同。 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. 命名结构在所有源文件中是相同的类型,因此是指向命名结构的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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