如何看待VS與cpluscplus com中is pod示例的不同輸出?

時間 2021-06-03 01:44:43

1樓:藍色

Visual Studio 2013 這裡是 C++98/03的體現,所以實現的不太好,畢竟應該是C++11了。

POD(Plain Old Data)在C++98/03標準中,其實是到處「散落」的,但是針對你的Case來說。首先根據C++03標準的9.4節來說,是這樣的:

A POD-struct is anaggregate classthat hasno 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 operatorand 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.non-static data members of type non-POD struct, non-POD union (or array of such types). Similarly, a POD union is a union that is both a trivial class and a standard-layout class, and has no non-static data members of type non-POD struct, non-POD union (or array of such types).

A POD classis a class that iseither a POD struct or a POD union.

這裡和C++03相比也不再是說是aggregate class,而是強調更具體的trival class和standard-layout class.

而對於trival class來說, 標準在第9章第6條也定義了

A trivial class is a class thathas a default constructor(12.1), has no non-trivial default constructors, and is trivially copyable

A trivially copyable class is a class that:

— has no non-trivial copy constructors (12.8),

— has no non-trivial move constructors (12.8),

— has no non-trivial copy assignment operators (13.5.3, 12.8),

— has no non-trivial move assignment operators (13.5.3, 12.8), and

— has a trivial destructor (12.4).

而standard-layout在第9章第7條為

A standard-layout class is a class that:

— has no non-static data members of type non-standard-layout class (or array of such types) or reference,

— has no virtual functions (10.3) and no virtual base classes (10.1),

— has the same access control (Clause 11) for all non-static data members,

— has no non-standard-layout base classes,

— either has no non-static data members in the most derived class and at most one base class with

non-static data members, or has no base classes with non-static data members, and

— has no base classes of the same type as the first non-static data member

於是只要可以滿足trival和standard-layout的在C++11中就可以是POD

而我之前加粗trivia class has a default constructor. 是想說只要有乙個default constructor,即使我們加上了自定義的constructor也是POD了,引用你的例子稍加改動

structA;

// C-struct (POD)

classB:

publicA};

// still POD

structC:

B};// still POD (member function)

structD:

C};// no POD (custom default constructor)

這裡依然可以得到B是POD的結果,因為B() = default是default constructor的,

然而如果是這樣

structA;

// C-struct (POD)

classB:

publicA};

// not POD

structC:

B};// not POD

structD:

C};// no POD (custom default constructor)

這個時候B就不是POD了,因為B(){}是自定義的constructor了

於是講到這裡,我想也應該懂得C++11的

std::is_trivial

std::is_trivially_copyable

std::is_standard_layout

為什麼叫這些名字了,這些名字也不是隨便取出來的,是有原因的。

如何看待熊孩子vs暴躁老哥?

大話了個西遊 打的狠了點,不過活該被打,沒教養。順便說下我的 有次去飯館吃飯,飯館老闆家有個小孩,也就這麼大吧。我和朋友坐在離飯館出飯口前面那張桌子 的 凳 子 上,離後堂門口和他們休息的屋子門口很近,小孩就拿個蒼蠅拍在那邊玩,我們在聊天,飯好了。吃了沒幾口那小孩就拿蒼蠅拍拍我 我就說別亂玩,這東西...

如何看待 2016 2017 賽季雷霆VS湖人,尼克楊的絕殺?

死在諾坎普 前兩天湖人打灰熊,落後三分,灰熊又是發球不進給機會,又是發界外球失誤給機會,然而湖人還是選擇了乙個莫名其妙的提前十幾秒的漂移三分。原來,湖人的關鍵球是這樣處理的 誰拿到球誰扔or誰搶到球誰扔。 註冊還得起名 估計很多人都連動態圖都沒看只看了新聞就在叫好球。這是走步絕殺。接球後可以走兩步沒...

如何看待明天鵜鶘vs快船的比賽?

NBA常規賽 鵜鶘VS快船賽事前瞻 主隊 快船 快船上一場比賽90 105不敵勇士,近5場只取得2勝,總戰績11勝8負西部排名第4。快船近況不佳,在巴圖姆缺陣的情況下只取得兩勝四負的戰績,上一場在主場迎戰全聯盟戰績最佳的勇士,上半場還能靠著優異的防守限制住勇士的得分咬住分差,但終究因為三分命中率不佳...