- 論壇徽章:
- 0
|
回復(fù) 8# linuxwyc
起先我也覺得奇怪,char arr[];這樣的成員聲明能通過嗎?看lz的代碼似乎是通過了,所以也就算了。
后來查了一下,數(shù)組聲明不指定bound,那么該數(shù)組的類型是incomplete type。C++白皮書3.9說:
A class that has been declared but not defined, or an array of unknown size or of incomplete element type, is an incompletely-defined object type. Incompletely-defined object types and the void types are incomplete types (3.9.1). Objects shall not be defined to have an incomplete type.
也就是說這樣的成員聲明是不符合標(biāo)準(zhǔn)的。但是實際使用的時候,gcc和vc都支持這種寫法,并且自動指定bound為0,也就是等效于char arr[0];,因為效果一樣,而且vc的警告都是“warning C4200: 使用了非標(biāo)準(zhǔn)擴(kuò)展 : 結(jié)構(gòu)/聯(lián)合中的零大小數(shù)組”。但是再參看白皮書8.3.4說:
In a declaration T D where D has the form
D1 [constant-expression opt]
... If the constant-expression(5.19) is present, it shall be an integral constant expression and its value shall be greater than zero.
所以bound為0也不符合標(biāo)準(zhǔn),所以vc給警告,但是允許。
而對于這種object with incomplete type,標(biāo)準(zhǔn)不允許,更不會說size是多少,所以他們的sizeof是unspecified的,我試了一下,vc下給的是1,和空結(jié)構(gòu)一樣,gcc給0,因為它支持這種用法,而且就是為了達(dá)到數(shù)組的越界或內(nèi)存的重疊的目的。 |
評分
-
查看全部評分
|