简体   繁体   English

typedef struct,但在关键字“struct”下保留新类型的命名空间

[英]typedef struct but keep new type namespaced under keyword 'struct'

How can I typedef a struct but still keep the new type namespaced under the keyword 'struct'?我怎样才能 typedef 一个结构,但仍将新类型命名为关键字'struct'下的命名空间?

example:例子:

struct foo {
    int x; 
}; 

typedef struct foo struct bar; 


int main(void) {
    struct bar bar; 
    bar.x = 10; 

    return 0; 
}

but this doesn't work obviously.但这显然不起作用。 Here are the errors if anyone is curious:如果有人好奇,这里是错误:

main.c:5:20: error: two or more data types in declaration specifiers
 typedef struct foo struct bar;
                    ^
main.c:5:27: warning: useless storage class specifier in empty declaration
 typedef struct foo struct bar;
                           ^
main.c: In function 'main':
main.c:9:13: error: storage size of 'bar' isn't known
  struct bar bar;

How can I typedef a struct but still keep the new type namespaced under the keyword 'struct'?我怎样才能 typedef 一个结构,但仍将新类型命名为关键字'struct'下的命名空间? . .

You cannot.你不能。 A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it.命名空间是一个声明性区域,它为其中的标识符(类型名称、function、变量等)提供 scope。 The concept of Namespace as it is defined within C++ , is not inherent in C .C++中定义的命名空间概念并不是C所固有的。

So, if you are okay with minor changes in your requirements, instead of doing something unnatural, use a simple typedef:因此,如果您对您的需求的微小更改感到满意,而不是做一些不自然的事情,请使用简单的 typedef:

Instead of this:而不是这个:

struct foo {
    int x; 
};  

do this:做这个:

typedef struct {
    int x; 
}foo;  

Then this will work:然后这将起作用:

typedef foo bar;

int main(void )
{   
    bar b;  
  
    b.x = 10;
    return 0;  
}

Note: Although namespaces are not inherent in in C , as they are in C++ , there are some interpretations eg: as discussed here , that argue the point.注意:虽然命名空间不是 C 中固有的,就像它们在CC++ ,但也有一些解释,例如: 如讨论here ,这证明了这一点。

How can I typedef a struct but still keep the new type namespaced under the keyword 'struct'?我怎样才能typedef一个结构,但仍将新类型命名为关键字'struct'下的命名空间?

So, it seems that you want to define a structure alias for another structure type.因此,您似乎想为另一种结构类型定义结构别名。

This is not possible with typedef as it creates a single word alias.这对于typedef是不可能的,因为它创建了一个单词别名。 The new alias can't be consisted of multiple white space separated words.新别名不能由多个空格分隔的单词组成。

But you can use a single name like struct_bar with struct implemented inside of the name to show that bar is a structure.但是您可以使用像struct_bar这样的单个名称,并在名称内部实现struct以表明bar是一个结构。

#include <stdio.h>

struct foo {
    int x; 
}; 

typedef struct foo struct_bar;


int main(void) {
    struct_bar bar;
    bar.x = 10; 

    return 0; 
}

You can't.你不能。 This isn't how it works.这不是它的工作方式。

You cannot "create" a type whose name is more than one word, nor can you refer to a type alias using the keyword struct .您不能“创建”名称超过一个单词的类型,也不能使用关键字struct引用类型别名。

The purpose of writing struct , in this context, is to refer to a struct type by a name that wasn't introduced as an alias.在这种情况下,编写struct的目的是通过未作为别名引入的名称来引用结构类型。 The keyword is there to say that that's what you want to do.关键字是说这就是你想要做的。 But it's not part of the name;但这不是名称的一部分 it cannot be part of the name.它不能是名称的一部分。

Fortunately, there's no reason to need or even want this.幸运的是,没有理由需要甚至想要这个。

C doesn't have any type of support for namespaces (at least not in the sense that C++ does). C 没有任何类型的命名空间支持(至少在 C++ 的意义上没有)。

When you create a typedef , the new type name is a single identifier, not multiple words.创建typedef时,新类型名称是单个标识符,而不是多个单词。 So struct bar can't be an alias for another type.所以struct bar不能是另一种类型的别名。 You would have to call it bar or some other one-word name.您必须将其称为bar或其他单字名称。

I found a solution that works for cygwin:我找到了一个适用于 cygwin 的解决方案:

struct foo {
    int x; 
}; 

struct bar {
    struct foo; 
};

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

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