• <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   管理


             
            国产精品久久久久a影院| 91精品国产91久久久久久蜜臀| 亚洲精品国产第一综合99久久| 狠狠色丁香久久婷婷综合_中| 影音先锋女人AV鲁色资源网久久 | 国产成人精品白浆久久69| 久久ZYZ资源站无码中文动漫| 热99re久久国超精品首页| 久久影视综合亚洲| 无码国内精品久久人妻蜜桃| 日本免费一区二区久久人人澡| 久久亚洲精品无码观看不卡| 一本久久a久久精品亚洲| 久久综合久久综合九色| 亚洲国产香蕉人人爽成AV片久久 | 精品久久久久久久中文字幕 | 国产成人精品白浆久久69| 久久AAAA片一区二区| 狠狠综合久久综合88亚洲 | 久久精品无码一区二区WWW | 亚洲AV日韩AV永久无码久久| 亚洲综合久久综合激情久久| 国内精品久久久久影院亚洲| 久久久久免费精品国产| 99精品国产免费久久久久久下载| 97久久精品人妻人人搡人人玩| 人人狠狠综合88综合久久| 国内精品久久久久久久97牛牛| 亚洲?V乱码久久精品蜜桃| 久久99精品国产一区二区三区| 久久这里只有精品首页| 99久久精品国产综合一区| 久久久无码精品亚洲日韩按摩| 亚洲日本va午夜中文字幕久久| 7777久久亚洲中文字幕| 久久国产色av免费看| 久久毛片免费看一区二区三区| 久久精品免费观看| 亚洲AV日韩AV永久无码久久| 国内精品久久国产| 久久久久国产一区二区|