• <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等多種語(yǔ)言 程序猿
            根據(jù)Loki的CheckReturn所說(shuō):
            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)制檢測(cè)函數(shù)返回值.
            loki提供的簡(jiǎn)單類CheckReturn提供了簡(jiǎn)單的機(jī)制去保證函數(shù)檢測(cè)返回值
            當(dāng)然可以使用定制的模板制定沒有檢測(cè)函數(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來(lái)顯示沒有檢測(cè)函數(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變量來(lái)存儲(chǔ)是否檢查了函數(shù)返回值
            如果CheckReturn r(value);r()一下則說(shuō)明檢查了函數(shù)返回值(非常有利于軟件測(cè)試)
            當(dāng)然檢測(cè)返回值的時(shí)期發(fā)生在其析構(gòu)過程中
            另外該類不允許對(duì)象的默認(rèn)構(gòu)造

            下面給出一個(gè)簡(jiǎn)單的使用例子:
             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ì)新思維
            不過想起來(lái)還是看其源碼和其自帶的使用例子
            關(guān)于loki庫(kù)我想寫多篇分析其庫(kù)的短小精悍的例子
            posted on 2010-04-05 14:19 ccsdu2009 閱讀(2306) 評(píng)論(12)  編輯 收藏 引用
            Comments

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


             
            91精品国产综合久久婷婷| 精品久久久久久99人妻| 久久超乳爆乳中文字幕| 日韩精品久久久肉伦网站| 久久艹国产| 99久久精品国产一区二区| 久久久亚洲欧洲日产国码是AV| 要久久爱在线免费观看| 国产激情久久久久影院小草| 亚洲国产成人乱码精品女人久久久不卡| 国产一级持黄大片99久久| 国产精品一区二区久久不卡| 狠狠狠色丁香婷婷综合久久俺| 久久人人爽人人人人爽AV| 91精品日韩人妻无码久久不卡| 久久精品国产一区二区三区日韩| 久久99精品国产99久久| 狠狠色噜噜狠狠狠狠狠色综合久久 | 久久这里有精品视频| 久久婷婷五月综合色99啪ak| 久久久久人妻一区二区三区| 久久精品中文字幕久久| 99久久99这里只有免费费精品| 中文字幕人妻色偷偷久久| 国产精品青草久久久久福利99| 久久丫精品国产亚洲av不卡| 久久综合丁香激情久久| 亚洲国产精品久久久久| 久久久久九国产精品| 无码人妻久久久一区二区三区| 久久国产高清字幕中文| 亚洲精品乱码久久久久久久久久久久 | 亚洲va久久久噜噜噜久久狠狠| 91精品国产91久久| 深夜久久AAAAA级毛片免费看| 国产精品99久久久久久www| 久久精品国产精品青草app| 人人狠狠综合久久亚洲高清| 久久综合九色综合久99| 精品久久久无码中文字幕| 国产精品一区二区久久精品无码|