天天看點

mysql aggregate是什麼類型_怎樣了解C++中的Aggregate和POD類型

展開全部

無論是 Aggregate 還是 POD(Aggregates的更新版)都是對用來限制初始化的精巧的概念。

motivation

當我們欲構造如62616964757a686964616fe58685e5aeb931333361306434下的union時:

union{    int i;    std::string s;

// illegal: std::string is not a POD type!} u;

vs 2013會對之作進一步的錯誤說明:

error C2621: “::s”: 非法的聯合成員;

類型“std::basic_string,std::allocator>”具有複制構造函數

兩者一規約,也即POD資料類型不允許使用其具有複制構造函數;

而且我們還可看到:

所謂的std::string類型其實是對std::basic_string<>某一特化版本的類型定義:

typedef basic_string, allocator >    string;

解釋:Aggregates vs POD

如果想深入了解 POD(Plain Old Data)的話,了解 aggregates(聚合,內建?) 是必須的。

首先我們來看C++标準對 aggregates 的定義:An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).

也即,首先所有的數組都是Aggregates,然後類類型如果滿足那四個條件,等等,标準未對結構體(structs)以及聯合體(unions)做任何說明。難道它們就不能成為aggregates了嗎?可以的。在C++中,術語(關鍵字) class 表示所有的類、結構體以及聯合。

那麼,這四條關于類(class)的限制說明了:

并非意味着一個 aggregate class 不可以擁有構造函數,事實上,它們可以擁有一個預設構造函數(default constrctor)以及(或者)一個拷貝構造函數隻有它們被編譯器隐式地聲明,而非被使用者顯式地聲明。

no private or protected non-static data members。You can have as many private and protected member functions (but not constructors) as well as as many private or protected static data members and member functions as you like and not violate the rules for aggregate classes。靜态成員可以出現在任何位置,非靜态成員隻得以公共成員(public)的形式出現。

An aggregate class can have a user-declared/user-defined copy-assignment operator and/or destructor。結合第一條,也即如果想要出現構造函數的話,必須以拷貝構造的形式出現(operator=),

An array is an aggregate even if it is an array of non-aggregate class type.

下面我們來看一些具體的例子:

class NotAggregate1{    virtual void f() {}    // no virtual functions};class NotAggregate2{    int x;                 // x is private by default and non-static    };class NotAggregate3{public:    NotAggregate3(int) {}   // user-defined ctor    };class Aggregate1{public:    NotAggregate1 member1;                                    // ok, public member    Aggregate1& operator(const Aggregate1& rhs) {// }                                // ok, copy-assignmentprivate:    void f() {}                                // ok, non-virtual function};

現在我們來說明,為什麼會提出 Aggregate 這樣一個概念以及它是如何跟 POD(Plain Old Data)發生關聯的?Aggregate 類型的類,可以被初始化通過 {},而non-Aggregate 的 class 不可以。

(提出這樣一個概念,是為了規範化初始化的用法):struct X{    int i1;    int i2;};struct Y{    char c;    X x;    int i[2];    float f;protected:    static double d;    Y& operator=(const Y& rhs) { return *this};private:    void g(){}};

我們可直接使用{}進行初始化:

Y y = {'a', {10, 10}, {20, 30}};            // y.c = 'a'            // y.x.i1 = 10            // y.x.i2 = 10            // y.i[0] = 20            // y.i[1] = 30            // y.f = 0.0 by value-initialized 的方式            // d 未被初始化,因為它是靜态的

POD

來自C++标準:

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.

An aggregate class is called a POD if it has no user-defined copy-assignment operator and destructor and none of its nonstatic members is a non-POD class, array of non-POD, or a reference.

所有的POD類都必須是Aggregate,或者說,一個類如果不是Aggregate,則一定不是POD類(兩者成逆否命題)。

和 Aggregates 的情況一樣,靜态成員不在乎其具體是什麼樣的類型(隻要是靜态的即可)