問題描述
我在現有代碼庫中有以下(工作)代碼,在 C 和 C++ 之間共享的包含文件中使用,在 MSVC (2010) 和 Windows DDK 上編譯:
I have the following (working) code in an existing code base, used in include file that is shared between C and C++, compiling on MSVC (2010) and Windows DDK:
struct X {
USHORT x;
} typedef X, *PX;
還有:
enum MY_ENUM {
enum_item_1,
enum_item_2
} typedef MY_ENUM;
據我所知,正確的定義應該是這樣的:
As far as I know, correct definition should look like this:
typedef struct {
USHORT x;
} X, *PX;
下面的表格有什么目的嗎?我錯過了什么嗎?
Is there any purpose for having the form below? Am I missing something?
推薦答案
typedef
和
是有效的,僅來自語言語法定義.
The fact that both typedef <type> <alias>
and <type> typedef <alias>
are valid simply comes from the language grammar definition.
typedef
被歸類為 storage-class specfifier(就像 static
、auto
),并且類型本身被稱為類型說明符.從標準第 6.7 節中的語法定義中,您會看到這些可以自由互換:
typedef
is classified as a storage-class specfifier (just like static
, auto
), and the type itself is known as the type-specifier. From the syntax definitions in section 6.7 of the standard, you'll see that these are free to be interchanged:
declaration:
declaration-specifiers init-declarator-list ;
declaration-specifiers:
storage-class-specifier declaration-specifiers
type-specifier declaration-specifiers
type-qualifier declaration-specifiers
function-specifier declaration-specifiers
init-declarator-list:
init-declarator
init-declarator-list , init-declarator
init-declarator:
declarator
declarator = initializer
(當然,請注意,這對于結構體和非結構體同樣適用,這意味著 double typedef 麻煩;
也是有效的.)
(Note, of course, that this is equally true for structs and for non-structs, meaning that double typedef trouble;
is also valid.)
這篇關于`struct X typedef` 與 `typedef struct X` 的含義是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!