• <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++高級工程師 Android高級軟件工程師 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)置的機制去強制檢測函數(shù)返回值.
            loki提供的簡單類CheckReturn提供了簡單的機制去保證函數(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 };
            可以看出對于軟件開發(fā)
            在release代碼中我們可以使用IgnoreReturnValue
            在debug代碼中我們可以使用ThrowLogicError來顯示沒有檢測函數(shù)返回值
            在CheckReturn類的實現(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 };
            首先它提供了一個bool變量來存儲是否檢查了函數(shù)返回值
            如果CheckReturn r(value);r()一下則說明檢查了函數(shù)返回值(非常有利于軟件測試)
            當然檢測返回值的時期發(fā)生在其析構過程中
            另外該類不允許對象的默認構造

            下面給出一個簡單的使用例子:
             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++設計新思維
            不過想起來還是看其源碼和其自帶的使用例子
            關于loki庫我想寫多篇分析其庫的短小精悍的例子
            posted on 2010-04-05 14:19 ccsdu2009 閱讀(2299) 評論(12)  編輯 收藏 引用
            Comments
            • # re: loki技法(2).CheckReturn
              欣萌
              Posted @ 2010-04-06 10:47
              受不了你了。
              你就不能換個顏色寫。  回復  更多評論   
            • # re: loki技法(2).CheckReturn
              陳梓瀚(vczh)
              Posted @ 2010-04-06 10:57
              @欣萌
              我認為受不了這種代碼的顏色配置是你自己的問題,這可是使用人數(shù)最多的。  回復  更多評論   
            • # re: loki技法(2).CheckReturn
              ccsdu2009
              Posted @ 2010-04-06 11:14
              不過我覺得這種方法實際意義似乎很小  回復  更多評論   
            • # re: loki技法(2).CheckReturn
              陳梓瀚(vczh)
              Posted @ 2010-04-06 11:54
              @ccsdu2009
              沒看明白要檢查什么……  回復  更多評論   
            • # re: loki技法(2).CheckReturn
              空明流轉
              Posted @ 2010-04-06 12:32
              @陳梓瀚(vczh)
              看明白了,但是覺得米意義。  回復  更多評論   
            • # re: loki技法(2).CheckReturn
              陳梓瀚(vczh)
              Posted @ 2010-04-06 12:36
              @空明流轉
              難道是檢查復制返回值的時候有沒有出錯?  回復  更多評論   
            • # re: loki技法(2).CheckReturn
              欣萌
              Posted @ 2010-04-06 13:42
              很想看看 什么情況下 ,這個會有實際的意義  回復  更多評論   
            • # re: loki技法(2).CheckReturn
              ccsdu2009
              Posted @ 2010-04-06 14:28
              @欣萌
              據(jù)我個人看法 這個應該于代碼測試有關
              如果測試的時候檢查了函數(shù)返回值則不會有問題
              如果沒有檢測函數(shù)返回值則根據(jù)定制規(guī)則做處理  回復  更多評論   
            • # re: loki技法(2).CheckReturn[未登錄]

              Posted @ 2010-04-07 10:25
              并非運用模板技術提供的編譯時錯誤提示吧。
              那就沒多大意義。
              運行時的話,啥都可以提供判斷咯。  回復  更多評論   
            • # re: loki技法(2).CheckReturn
              ccsdu2009
              Posted @ 2010-04-07 14:16
              @~
              似乎不大可能使用模板編譯時提供代碼測試吧  回復  更多評論   
            • # re: loki技法(2).CheckReturn
              欲三更
              Posted @ 2010-04-09 03:09
              debug的時候用的吧?  回復  更多評論   
            • # re: loki技法(2).CheckReturn
              ccsdu2009
              Posted @ 2010-04-13 19:19
              @欲三更
              不一定  回復  更多評論   
             
            久久久久久久综合狠狠综合| 人妻无码精品久久亚瑟影视| 久久久国产亚洲精品| 国产精品久久久久一区二区三区| 一本一本久久aa综合精品| 2021国产精品久久精品| 亚洲欧美另类日本久久国产真实乱对白| 日本久久久精品中文字幕| 久久综合中文字幕| 国产激情久久久久影院| 久久久久亚洲AV无码专区网站| 亚洲一区二区三区日本久久九| 伊人久久免费视频| 精品无码久久久久久国产| 久久99精品免费一区二区| 亚洲AⅤ优女AV综合久久久| 2021国产精品久久精品| 久久99精品国产自在现线小黄鸭| 久久亚洲AV成人无码国产| 国产亚洲欧美成人久久片| 国产成人精品久久亚洲| 亚洲国产精品综合久久网络 | 亚洲精品国精品久久99热| 色天使久久综合网天天| 国产亚洲精品美女久久久| 91精品日韩人妻无码久久不卡| 亚洲午夜久久久| 国产美女久久精品香蕉69| 久久无码人妻精品一区二区三区| 少妇熟女久久综合网色欲| 久久青青草原综合伊人| 亚洲AV伊人久久青青草原| 久久99热只有频精品8| 久久久久国产精品嫩草影院| 亚洲AV乱码久久精品蜜桃| 国产亚洲精午夜久久久久久| 亚洲国产欧洲综合997久久| 亚洲一区二区三区日本久久九| 久久久久久精品无码人妻| 色综合久久久久网| 亚洲精品tv久久久久久久久 |