第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么c++拋出異常后還能對函數(shù)內(nèi)的局部對象進(jìn)行析構(gòu)?

為什么c++拋出異常后還能對函數(shù)內(nèi)的局部對象進(jìn)行析構(gòu)?

胡說叔叔 2019-03-30 11:31:06
C++是如何確保出了異常還能調(diào)用析構(gòu)函數(shù)的
查看完整描述

2 回答

?
米脂

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個贊

如何確保?標(biāo)準(zhǔn)確保。因?yàn)檫@是標(biāo)準(zhǔn)規(guī)定的。
以下摘自C++11Standard(draftN3690)
15.2Constructorsanddestructors[except.ctor]
不光保證了被析構(gòu),還規(guī)定了析構(gòu)的順序。
1)Ascontrolpassesfromthepointwhereanexceptionisthrowntoahandler,destructorsareinvokedforallautomaticobjectsconstructedsincethetryblockwasentered.Theautomaticobjectsaredestroyedinthereverseorderofthecompletionoftheirconstruction.
比較特殊一點(diǎn)的,如果異常發(fā)生在構(gòu)造或析構(gòu)的時候,其子對象也能確保被正確的析構(gòu)。而該對象本身呢?構(gòu)造的時候它還不存在呢,所以無須擔(dān)心。析構(gòu)的情況會在后面說明。
2)Anobjectofanystoragedurationwhoseinitializationordestructionisterminatedbyanexceptionwillhavedestructorsexecutedforallofitsfullyconstructedsubobjects(excludingthevariantmembersofaunion-likeclass),thatis,forsubobjectsforwhichtheprincipalconstructor(12.6.2)hascompletedexecutionandthedestructorhasnotyetbegunexecution.Similarly,ifthenon-delegatingconstructorforanobjecthascompletedexecutionandadelegatingconstructorforthatobjectexitswithanexception,theobject’sdestructorwillbeinvoked.Iftheobjectwasallocatedinanew-expression,thematchingdeallocationfunction(3.7.4.2,5.3.4,12.5),ifany,iscalledtofreethestorageoccupiedbytheobject.
這整個過程被稱為"stackunwinding",翻譯過來叫:堆棧輾轉(zhuǎn)開解。
3)Theprocessofcallingdestructorsforautomaticobjectsconstructedonthepathfromatryblocktothepointwhereanexceptionisthrowniscalled“stackunwinding.”Ifadestructorcalledduringstackunwindingexitswithanexception,std::terminateiscalled(15.5.1).[Note:Sodestructorsshouldgenerallycatchexceptionsandnotletthempropagateoutofthedestructor.—endnote]
注意看那個“Note”,標(biāo)準(zhǔn)對編譯器做了要求,對于析構(gòu)函數(shù)來說,是要對自身負(fù)責(zé)的。
正是因?yàn)閟tackunwinding的保證作為基礎(chǔ),才有了我們所熟知的RAII技術(shù)。
另外可以注意到,如果在stackunwinding期間拋出異常呢?就只能調(diào)用std::terminate了:
15.5.1Thestd::terminate()function[except.terminate]
2)Insuchcases,std::terminate()iscalled(18.8.3).Inthesituationwherenomatchinghandlerisfound,itisimplementation-de?nedwhetherornotthestackisunwoundbeforestd::terminate()iscalled.Inthesituationwherethesearchforahandler(15.3)encounterstheoutermostblockofafunctionwithanoexcept-speci?cationthatdoesnotallowtheexception(15.4),itisimplementation-de?nedwhetherthestackisunwound,unwoundpartially,ornotunwoundatallbeforestd::terminate()iscalled.Inallothersituations,thestackshallnotbeunwoundbeforestd::terminate()iscalled.Animplementationisnotpermittedto?nishstackunwindingprematurelybasedonadeterminationthattheunwindprocesswilleventuallycauseacalltostd::terminate()
stackunwinding的過程并不保證做完,但最終肯定是要調(diào)用std::terminate來終止。
                            
查看完整回答
反對 回復(fù) 2019-03-30
?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個贊

在拋出異常后調(diào)用棧內(nèi)存對象的析構(gòu)函數(shù),在C++標(biāo)準(zhǔn)里有規(guī)定。析構(gòu)函數(shù)本來就不是顯式調(diào)用的,編譯器和運(yùn)行環(huán)境自然知道什么時候應(yīng)該調(diào)用析構(gòu)函數(shù),只要它們是按照C++標(biāo)準(zhǔn)實(shí)現(xiàn)的。至于如何實(shí)現(xiàn),我想也不難吧,只需要給初始化過的對象做個標(biāo)記,處理異常的時候逐個調(diào)用它們的析構(gòu)函數(shù)不就好了。
                            
查看完整回答
反對 回復(fù) 2019-03-30
  • 2 回答
  • 0 關(guān)注
  • 646 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號