<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C++核心準(zhǔn)則E.25:如果不能拋出異常,模仿RAII方式進(jìn)行資源管理

          月季

          E.25: If you can't throw exceptions, simulate RAII for resource management

          E.25:如果不能拋出異常,模仿RAII方式進(jìn)行資源管理


          Reason(原因)

          Even without exceptions,?RAII?is usually the best and most systematic way of dealing with resources.

          即使不和異常一起使用,RAII通常是最好的,最系統(tǒng)化的處理資源的方式。


          Note(注意)

          Error handling using exceptions is the only complete and systematic way of handling non-local errors in C++. In particular, non-intrusively signaling failure to construct an object requires an exception. Signaling errors in a way that cannot be ignored requires exceptions. If you can't use exceptions, simulate their use as best you can.

          使用異常的錯誤處理是C++中完全、系統(tǒng)化處理非局部錯誤的唯一方式。通常,非侵入式發(fā)出錯誤信號以便構(gòu)造一個對象需要使用異常。以無法忽視的方式發(fā)出錯誤信號需要異常。如果你無法使用異常,盡你所能模擬它。

          A lot of fear of exceptions is misguided. When used for exceptional circumstances in code that is not littered with pointers and complicated control structures, exception handling is almost always affordable (in time and space) and almost always leads to better code. This, of course, assumes a good implementation of the exception handling mechanisms, which is not available on all systems. There are also cases where the problems above do not apply, but exceptions cannot be used for other reasons. Some hard-real-time systems are an example: An operation has to be completed within a fixed time with an error or a correct answer. In the absence of appropriate time estimation tools, this is hard to guarantee for exceptions. Such systems (e.g. flight control software) typically also ban the use of dynamic (heap) memory.

          很多關(guān)于異常的大量恐懼都是被誤導(dǎo)的。當(dāng)在沒有被指針或復(fù)雜的控制結(jié)構(gòu)搞亂的代碼環(huán)境中使用異常時,異常處理幾乎總是可以接受的(無論是時間還是空間維度),幾乎總是可以帶來更好的代碼。當(dāng)然,想象一個異常處理機(jī)制的良好實現(xiàn),它不存在于任何系統(tǒng)。還是存在不屬于上述問題,但由于其他原因而不能使用異常的情況。某些硬實時系統(tǒng)就是例子之一:一個操作必須在固定時間內(nèi)完成并得到正確或錯誤的結(jié)果。如果沒有適當(dāng)?shù)臅r間評價工具,異常很難滿足這個要求。這樣的系統(tǒng)(例如飛行控制系統(tǒng))通常也會禁止使用動態(tài)(堆)內(nèi)存。

          So, the primary guideline for error handling is "use exceptions and?RAII." This section deals with the cases where you either do not have an efficient implementation of exceptions, or have such a rat's nest of old-style code (e.g., lots of pointers, ill-defined ownership, and lots of unsystematic error handling based on tests of error codes) that it is infeasible to introduce simple and systematic exception handling.

          Before condemning exceptions or complaining too much about their cost, consider examples of the use of?error codes. Consider the cost and complexity of the use of error codes. If performance is your worry, measure.

          因此,關(guān)于錯誤處理,主要的準(zhǔn)則是“使用異常和RAII”。這個部分內(nèi)容處理以下情況:要么你無法獲得異常的高效實現(xiàn),要么你的代碼是老鼠窩一樣的舊代碼(例如,大量的指針,病態(tài)定義的所有權(quán),大量的基于測試錯誤的非系統(tǒng)化錯誤處理),這時無法引入簡單和系統(tǒng)化的異常處理。在譴責(zé)異常或抱怨異常的成本過高之前,考慮使用錯誤代碼時的成本和復(fù)雜度。如果你擔(dān)心性能,進(jìn)行測量(而不是無根據(jù)的懷疑,譯者注)。


          Example(示例)

          Assume you wanted to write

          假設(shè)你想編寫如下代碼:

          void func(zstring arg)
          {
          Gadget g {arg};
          // ...
          }

          If the?gadget?isn't correctly constructed,?func?exits with an exception. If we cannot throw an exception, we can simulate this RAII style of resource handling by adding a?valid()?member function to?Gadget:

          如果gadget無法正確構(gòu)建,func會因為異常退出。如果你無法拋出異常,我們可以通過給Gadget增加一個valid成員函數(shù)來模擬RAII風(fēng)格的資源管理。

          error_indicator func(zstring arg)
          {
          Gadget g {arg};
          if (!g.valid()) return gadget_construction_error;
          // ...
          return 0; // zero indicates "good"
          }

          The problem is of course that the caller now has to remember to test the return value.

          這里的問題當(dāng)然是調(diào)用者必須記得檢查返回值。

          See also:?Discussion

          參見:https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Sd-???


          Enforcement(實施建議)

          Possible (only) for specific versions of this idea: e.g., test for systematic test of?valid()?after resource handle construction

          可能(僅僅)適用這個想法的特定版本:例如,在資源句柄構(gòu)建之后系統(tǒng)化檢查valid()。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e25-if-you-cant-throw-exceptions-simulate-raii-for-resource-management


          新書介紹

          以下是本人3月份出版的新書,拜托多多關(guān)注!


          本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設(shè)計模式逐個進(jìn)行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設(shè)計模式的運(yùn)用場景和想要解決的問題;另一方面通過對這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時如何判斷使用設(shè)計模式的利弊,并合理運(yùn)用設(shè)計模式。

          對設(shè)計模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運(yùn)用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計和開發(fā)的參考;使用Python 語言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。




          覺得本文有幫助?請分享給更多人。

          關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!

          面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>



          瀏覽 48
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产久久福利导航 | 激情草逼| 福利第一页| 国产18女人水真多免费看 | 久草中文在线播放 |