• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            Cpper
            C/C++高級(jí)工程師 Android高級(jí)軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語言 程序猿
            根據(jù)Loki的CheckReturn所說:
            1 ///  C++ provides no mechanism within the language itself to force code to
            2 ///  check the return value from a function call.  This simple class provides
            3 ///  a mechanism by which programmers can force calling functions to check the
            4 ///  return value.  Or at least make them consciously choose to disregard the
            5 ///  return value.  If the calling function fails to use or store the return
            6 ///  value, the destructor calls the OnError policy.
            c++并沒有提供內(nèi)置的機(jī)制去強(qiáng)制檢測函數(shù)返回值.
            loki提供的簡單類CheckReturn提供了簡單的機(jī)制去保證函數(shù)檢測返回值
            當(dāng)然可以使用定制的模板制定沒有檢測函數(shù)返回值時(shí)的處理策略
            1.CheckReturn的處理策略(內(nèi)置)
                
             1 template<class T>
             2 struct IgnoreReturnValue
             3 {
             4     static void run(const T&)
             5     {
             6         /// Do nothing at all.
             7     }
             8 };
             9 
            10 template<class T>
            11 struct ThrowTheValue
            12 {
            13     static void run(const T & value )
            14     {
            15         throw value;
            16     }
            17 };
            18 
            19 template<class T>
            20 struct ThrowLogicError
            21 {
            22     static void run( const T & )
            23     {
            24         throw ::std::logic_error( "CheckReturn: return value was not checked.\n" );
            25     }
            26 };
            27 
            28 template<class T>
            29 struct TriggerAssert
            30 {
            31     static void run(const T&)
            32     {
            33         assert( 0 );
            34     }
            35 };
            36 
            37 template<class T>
            38 struct FprintfStderr
            39 {
            40     static void run(const T&)
            41     {
            42         fprintf(stderr, "CheckReturn: return value was not checked.\n");
            43     }
            44 };
            可以看出對(duì)于軟件開發(fā)
            在release代碼中我們可以使用IgnoreReturnValue
            在debug代碼中我們可以使用ThrowLogicError來顯示沒有檢測函數(shù)返回值
            在CheckReturn類的實(shí)現(xiàn)如下:
             1 template < class Value , template<class> class OnError = TriggerAssert >
             2 class CheckReturn
             3 {
             4 public:
             5 
             6     /// Conversion constructor changes Value type to CheckReturn type.
             7     inline CheckReturn( const Value & value ) :
             8         m_value( value ), m_checked( false ) {}
             9 
            10     /// Copy-constructor allows functions to call another function within the
            11     /// return statement.  The other CheckReturn's m_checked flag is set since
            12     /// its duty has been passed to the m_checked flag in this one.
            13     inline CheckReturn( const CheckReturn & that ) :
            14         m_value( that.m_value ), m_checked( false )
            15     { that.m_checked = true; }
            16 
            17     /// Destructor checks if return value was used.
            18     inline ~CheckReturn( void )
            19     {
            20         // If m_checked is false, then a function failed to check the
            21         // return value from a function call.
            22         if (!m_checked)
            23             OnError<Value>::run(m_value);
            24     }
            25 
            26     /// Conversion operator changes CheckReturn back to Value type.
            27     inline operator Value ( void )
            28     {
            29         m_checked = true;
            30         return m_value;
            31     }
            32 
            33 private:
            34     /// Default constructor not implemented.
            35     CheckReturn( void );
            36 
            37     /// Copy-assignment operator not implemented.
            38     CheckReturn & operator = ( const CheckReturn & that );
            39 
            40     /// Copy of returned value.
            41     Value m_value;
            42 
            43     /// Flag for whether calling function checked return value yet.
            44     mutable bool m_checked;
            45 };
            首先它提供了一個(gè)bool變量來存儲(chǔ)是否檢查了函數(shù)返回值
            如果CheckReturn r(value);r()一下則說明檢查了函數(shù)返回值(非常有利于軟件測試)
            當(dāng)然檢測返回值的時(shí)期發(fā)生在其析構(gòu)過程中
            另外該類不允許對(duì)象的默認(rèn)構(gòu)造

            下面給出一個(gè)簡單的使用例子:
             1 #include <loki/CheckReturn.h>
             2 
             3 using namespace std;
             4 using namespace Loki;
             5 
             6 //#define G_DEBUG 
             7 
             8 #ifndef G_DEBUG 
             9 typedef CheckReturn<int,FprintfStderr> CheckInt;  
            10 #else
            11 typedef CheckReturn<int,IgnoreReturnValue> CheckInt;
            12 #endif 
            13 
            14 int main(int argc, char *argv[])
            15 {   
            16     int i = 0;
            17     {
            18        CheckInt check(i);
            19     }
            20     
            21     system("PAUSE");
            22     return EXIT_SUCCESS;
            23 }
            如果我們定義了宏G_DEBUG則表明這是debug模式
             
            附注:以前早早就接觸到了loki,以前也翻過c++設(shè)計(jì)新思維
            不過想起來還是看其源碼和其自帶的使用例子
            關(guān)于loki庫我想寫多篇分析其庫的短小精悍的例子
            posted on 2010-04-05 14:19 ccsdu2009 閱讀(2299) 評(píng)論(12)  編輯 收藏 引用
            Comments

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


             
            日日狠狠久久偷偷色综合免费| 精品国产乱码久久久久软件| 九九精品99久久久香蕉| 久久亚洲精品中文字幕三区| 久久久精品免费国产四虎| 久久青青草原亚洲av无码| 中文字幕日本人妻久久久免费| 色偷偷88888欧美精品久久久| 97久久精品无码一区二区| 久久99精品国产自在现线小黄鸭 | 久久综合色老色| 少妇精品久久久一区二区三区| 久久精品国产只有精品66 | 久久亚洲美女精品国产精品| 18岁日韩内射颜射午夜久久成人| 亚洲美日韩Av中文字幕无码久久久妻妇| 麻豆精品久久久久久久99蜜桃| 精品久久久久中文字| 久久精品毛片免费观看| 久久亚洲中文字幕精品一区| 久久亚洲国产午夜精品理论片| 午夜人妻久久久久久久久| 亚洲国产高清精品线久久| 97精品伊人久久久大香线蕉 | 久久婷婷色综合一区二区| 久久激情亚洲精品无码?V| 久久亚洲精品中文字幕三区| 2021少妇久久久久久久久久| 久久精品国产亚洲AV无码麻豆| 亚洲AV日韩AV永久无码久久| 久久精品一本到99热免费| 精品国产日韩久久亚洲| 亚洲精品国产第一综合99久久| 久久精品夜色噜噜亚洲A∨| 成人精品一区二区久久| 久久国产精品-久久精品| 久久九九亚洲精品| 伊人久久免费视频| 久久亚洲精品无码播放| 欧美国产精品久久高清| 免费精品久久久久久中文字幕|