什麼是POD?這是一個問題.我甚至很難找到2份完全相同的答案.
摘自文檔ISO/IEC 14882:2003(E) P153:
...A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct,
non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator
and no user-defined destructor. Similarly, a POD-union is an aggregate union that has no non-static data
members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-
defined copy assignment operator and no user-defined destructor. A POD class is a class that is either a
POD-struct or a POD-union.
(我連換行什麼的格式都一併拷貝了下來)
然而我們的wiki百科在上述這段話的背後又加註了一下面段話,讓我更疑惑了.注意我上面貼的那段話也幾乎出現在維基中的這段話前面,我說幾乎,是個別措辭有些區別並且沒有提union,但是意思可以說是不多不少,恰好等價.
http://en.wikipedia.org/wiki/Plain_Old_Data_Structures
(A POD type in C++ is defined as either a scalar type or a POD class. A POD class has no user-defined copy assignment operator, no user-defined destructor, and no non-static data members that are not themselves PODs.)Moreover, a POD class must be an aggregate, meaning it has no user-declared constructors, no private nor protected non-static data, no bases and no virtual functions.
這個Moreover(我覺得該Morever的)就徹底讓我糊塗了:這是維基志願編輯者畫蛇添足呢,還是真的在ISO文檔中確實有這段話,我自己沒有找到.因為第一段話我也僅僅是在一個介紹Class的角落中無意找到了,從編撰的格式上來說不像正式定義, 措辭上也僅僅用了"that" 這樣的描述性詞彙,似乎是在描述其部分特性而不是嚴格的對其進行定義.
不過可以肯定的是,在我們通常用的編譯器中,有虛函數的肯定不是POD.這一點讓我有些懷疑POD是一個實現定義的標準,標準僅僅給出了其最小要求,然後具體的各個編譯器根據其物件模型還各自加了一些要求.
我們不能否定可能存在某個編譯器,在其虛函數的實現模型中沒有在對象中插入資料,也使得其構造過程不需要設定虛表指標之類的( 全域表?). 也不能排除某個編譯器可以做出超牛的inline最佳化, 讓建構函式的執行隱藏在了某些C語言標準中的未定義行為中(純屬筆者臆想哈).不然我也實在沒法給個理由讓自己相信:需要初始化才能用的對象居然還是POD?
我見過的最常見的POD要求是(個人總結):
1.ISO標準中的要求.
2.沒有虛函數和自訂的任何建構函式. ISO標準只禁止了重載operator "="和解構函式.
3.沒有基類.
4.沒有非靜態常量和參考型別成員. 這2個玩意兒沒有自訂建構函式沒法初始化.
5.無虛函數和自訂的任何建構函式的空類是非POD, 繼承空類不會影響子類是否是POD的判斷. 空類被打成非POD我懷疑僅是為了允許參數傳遞和傳回值最佳化.
C++ 0x標準草案最新POD定義(好像已經投票通過了):
http://en.wikipedia.org/wiki/C%2B%2B0x
A class/struct is considered a POD if it is trivial, standard-layout, and if all of its non-static members are PODs.
A trivial class or struct is defined as one that:
- Has a trivial default constructor. This may use the default constructor syntax (
SomeConstructor() = default;).
- Has a trivial copy constructor, which may use the default syntax.
- Has a trivial copy assignment operator, which may use the default syntax.
- Has a trivial destructor, which must not be virtual.
A standard-layout class or struct is defined as one that:
- Has only non-static data members that are of standard-layout type
- Has the same access control (public, private, protected) for all non-static data members
- Has no virtual functions
- Has no virtual base classes
- Has only base classes that are of standard-layout type
- Has no base classes of the same type as the first defined non-static data member
- Either has no base classes with non-static data members, or has no non-static data members in the most derived class and at most one base class with non-static data members. In essence, there may be only one class in this class's hierarchy that has non-static data members.
中文版:http://zh.wikipedia.org/zh-cn/C%2B%2B0x#.E5.B0.8DPOD.E5.AE.9A.E7.BE.A9.E7.9A.84.E4.BF.AE.E6.AD.A3
當class/struct是極簡的(trivial)、屬於標準布局(standard-layout),以及他的所有非靜態(non-static)成員都是POD時,會被視為POD。
一個極簡的類或結構符合以下定義:
- 極簡的預設建構式。這可以使用預設建構式文法,例如SomeConstructor() = default;
- 極簡的複製建構式,可使用預設文法(default syntax)
- 極簡的賦值操作符,可使用預設文法(default syntax)
- 極簡的解構式,不可以是虛擬(virtual)
一個標準布局(standard-layout)的類或結構符合以下定義:
- 只有非靜態(non-static)資料成員,且這些成員也是符合標準布局的類型
- 對所有non-static成員有相同的存取控制(public, private, protected)
- 沒有虛函數
- 沒有虛擬基類
- 只有符合標準布局的基類
- 沒有和第一個定義的non-static成員相同類型的基類
- 若非沒有帶有non-static成員的基類,就是最底層(繼承最末位)的類沒有non-static資料成員而且至多一個帶有non-static成員的基類。基本上,在該類的繼承體系中只會有一個類帶有non-static成員
個人觀點, 僅供參考. 歡迎達人指點.------------------------------------------------補正----------------------------------------------------------------
...A POD-struct is an aggregate class that:經過高人指點,標準中這句話的"that"使用不當,應當替換為"and"!
也就是說後面的從句不是解釋aggregate 而是 what's more的意思!
aggregate 的定義:An aggregate class is a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions。