__typeof__ 運算子
IBM® 延伸開始。
__typeof__
運算子會傳回其引數的類型,可以是表示式或類型。 語言特性提供從表示式衍生類型的方法。 給定表示式 e
,可在任何需要類型名稱的地方使用 __typeof__(e)
,例如在宣告或強制轉型中。
__typeof__
建構本身不是表示式,而是類型的名稱。 __typeof__
建構的行為與使用 __typedef__
所定義的類型名稱類似,雖然語法類似 sizeof
。下列範例說明其基本語法。 若為表示式
e
:int e;
__typeof__(e + 1) j; /* the same as declaring int j; */
e = (__typeof__(e)) f; /* the same as casting e = (int) f; */
使用
__typeof__
建構相當於宣告 typedef
名稱。 給定int T[2];
int i[2];
你可以寫__typeof__(i) a; /* all three constructs have the same meaning */
__typeof__(int[2]) a;
__typeof__(T) a;
程式碼的行為就像您已宣告 int a[2];
一樣。對於位元欄位, __typeof__
代表位元欄位的基礎類型。 例如, int m:2;
, __typeof__(m)
是 int
。 因為未保留位元欄位內容,所以 __typeof__(m)
n;
中的 n
與 int n
相同,但不是 int
n:2
。
__typeof__
運算子可以巢套在 sizeof
及其本身內。 arr
的下列宣告作為指向 int
的指標陣列是相等的:int *arr[10]; /* traditional C declaration */
__typeof__(__typeof__ (int *)[10]) a; /* equivalent declaration */
在表示式
e
是參數的巨集定義中, __typeof__
運算子可能很有用。 例如,#define SWAP(a,b) { __typeof__(a) temp; temp = a; a = b; b = temp; }
相關資訊
- 類型名稱
- typedef 定義
- ILE C/C++ 編譯器參照 中的 LANGLVL (*EXTENDED)
IBM 延伸結束。