c primer練習16 27怎麼判斷到底有沒有例項化?

時間 2021-06-06 17:32:55

1樓:三土土門人

也看到這裡搞不明白,剛看到[C++]類模板在何時例項化? - 若有所思 - C++部落格的驗證方法,就是把開頭Stack的定義改成宣告,也就是把大括號去掉,讓類模板的定義缺失。編譯後,c、e、f都出錯,顯示錯誤資訊都提到了incomplete type 'stack',所以我覺得答案應該是c、e、f都發生了例項化,而a、b、d都沒有例項化。

繼續瞎想。是否例項化好像和記憶體空間有關係。比如引用不分配記憶體,指標分配的記憶體應該都是同樣大小,宣告也不需要分配記憶體,所以它們都不需要知道類具體內容,不需要例項化。

而定義了乙個物件,或者函式值傳遞的時候,都需要分配記憶體,必須得知道類的細節,需要例項化。

額,以上純屬瞎想,我是學機械的,剛開始學C++,好多不懂的只能這麼自己編。。。

2樓:573xmcgcg

我查閱了一下標準文件,認為習題集的答案更靠譜。我先把標準文件相關的部分貼上來。

以下內容來自C++17草稿(N4606)第14.7.1小節(有刪節):

第1條:

Unless a class template specialization has been explicitly instantiated or explicitly specialized, the class template specialization is implicitly instantiated when the specialization is referenced in a context that requires acompletely-definedobject type or when the completeness of the class type affects the semantics of the program. [ Note: In particular, if the semantics of an expression depend on themember or base class lists of a class template specialization, the class template specialization is implicitly generated.

For instance, deleting a pointer to class type depends on whether or not the class declares a destructor, and a conversion between pointers to class type depends on the inheritance relationship between the two classes involved. —end note ]

第2條:

Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires themember definitionto exist; in particular, the initialization (and any associated side effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist.

第3條:

Unless a function template specialization has been explicitly instantiated or explicitly specialized, the function template specialization is implicitly instantiated when the specialization is referenced in a context that requires afunction definitionto exist. Unless a call is to a function template explicit specialization or to a member function of an explicitly specialized class template, a default argument for a function template or a member function of a class template is implicitly instantiated when the function is called in a context that requires the value of the default argument.以下是我的【個人理解】,可能有誤:

從以上3條可以看出,需要(隱式)例項化的理由一般是需要獲得模板函式/類的定義。

(a) f1 需要乙個 Stack,在 (e) 處呼叫時需要 Stack 的定義,會例項化。

(b) 引用不能為空,rsd 必須在它所在的類物件被構造時繫結到乙個 Stack 物件,此處需要 Stack 的定義,會例項化。

(c) 構造 Exercise 物件時需要為 si 分配空間,需要 Stack 的定義,會例項化。

(d) sc 並沒有指向任何乙個 Stack 物件,不需要 Stack 的定義,不會例項化。

(e) 例項化在編譯階段完成,在發現 f1 的呼叫後會在 (a) 處例項化 Stack(此處存疑)。

(f) sizeof 需要知道 Stack 的定義才能給出乙個 Stack 物件的大小,會例項化。

背景怎麼畫怎麼練習?

七虞 這個碑看起來詭異,主要是因為透視不對,近大遠小近高遠低近寬遠窄,碑調整一下就看著舒服多了。背景作為乙個空間對透視的要求會更多 平面背景除外 可以學一下透視,包括背景中對虛實的處理。 那個人的日常 這裡只提供捷徑和個人經驗,按主觀角度從易到難的順序講如何新增背景。我畫的菜不菜我心裡有數,不接受非...

透視怎麼練習啊?

畫學反應 我們得先了解透視的原理。在素描中最基本的形體是立方體。素描時,大多是以對三個面所進行的觀察方法來決定立方體的表現。另外,利用面與面的分界線所造成的角度,也能暗示出物體的深度,這就涉及到透視規律。透視分一點透視 又稱平行透視 兩點透視 又稱成角透視 及三點透視三類。1.一點透視 就是說立方體...

怎麼練習狗喘氣?

陳軍平 首先張開嘴,伸出自己的舌頭,然後開始吸氣 呼氣,逐漸由慢到快,這時會感到小腹在隨著氣息在鼓起。每次練習持續時間一到兩分鐘,每天堅持練習,時間逐漸增加 喘氣的速度逐漸加快。通過這一練習,可以鍛鍊氣息的控制能力 彈跳能力 支撐能力 持久能力等,極為有效。有了良好的氣息作為動力,接下來的問題便是開...