我在XCB API標頭中看到了以下奇怪現象。大多數類型都以這種方式使用類型別名和結構名稱來定義:Typedef有和沒有結構名稱
typedef struct xcb_window_iterator_t {
xcb_window_t *data;
int rem;
int index;
} xcb_window_iterator_t;
但是某些typedefs省略了結構名稱。
typedef struct {
unsigned int sequence;
} xcb_void_cookie_t;
從C的角度來看,這是什麼意思?
後者是一個名稱和結構沒有名稱的類型?或者它不是類型,而只是一個帶有名字的結構?
我的疑惑來自gccxml
的輸出。
對於xcb_window_iterator_t
它會產生一個Struct
節點:
$ xmllint --format --xpath '//Struct[@name="xcb_window_iterator_t"]' xcb.xml
<Struct id="_199" name="xcb_window_iterator_t" context="_1" mangled="21xcb_window_iterator_t" demangled="xcb_window_iterator_t" location="f0:43" file="f0" line="43" artificial="1" size="128" align="64" members="_2240 _2241 _2242 _2243 _2244 _2245 _2246 " bases=""/>
和Typedef
節點:
$ xmllint --format --xpath '//Typedef[@name="xcb_window_iterator_t"]' xcb.xml
<Typedef id="_200" name="xcb_window_iterator_t" type="_199" context="_1" location="f0:47" file="f0" line="47"/>
但xcb_void_cookie_t
其產生只是一個Struct
節點:
$ xmllint --format --xpath '//Struct[@name="xcb_void_cookie_t"]' xcb.xml
<Struct id="_967" name="xcb_void_cookie_t" context="_1" mangled="17xcb_void_cookie_t" demangled="xcb_void_cookie_t" location="f14:189" file="f14" line="189" size="32" align="32" members="_3655 _3656 _3657 _3658 _3659 " bases=""/>
但是,沒有Typedef
節點:
$ xmllint --format --xpath '//Typedef[@name="xcb_void_cookie_t"]' xcb.xml
XPath set is empty
這是否意味着xcb_void_cookie_t
是沒有類型,但只是一個結構,雖然代碼包含typedef
?或者它是gccxml
中的一個錯誤?
你不應該對的typedef結構在所有的麻煩。他們沒用。它們只會節省你在這裏和那裏輸入「struct」,但是混淆了一些結構,並且需要用'.'或' - >'來操作。 – Jens
@Jens:這是一個觀點 - 由尊敬的程序員持有。這不是唯一的觀點 - 其他受尊敬的程序員更喜歡替代方案。 –