Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
根据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++并没有提供内置的机制去强制检测函数返回值.
loki提供的简单类CheckReturn提供了简单的机制去保证函数检测返回值
当然可以使用定制的模板制定没有检测函数返回值时的处理策略
1.CheckReturn的处理策略(内置)
    
 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 };
可以看出对于软件开发
在release代码中我们可以使用IgnoreReturnValue
在debug代码中我们可以使用ThrowLogicError来显示没有检测函数返回值
在CheckReturn类的实现如下:
 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变量来存储是否检查了函数返回值
如果CheckReturn r(value);r()一下则说明检查了函数返回值(非常有利于软件测试)
当然检测返回值的时期发生在其析构过程中
另外该类不允许对象的默认构造

下面给出一个简单的使用例子:
 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 阅读(2259) 评论(12)  编辑 收藏 引用
Comments
  • # re: loki技法(2).CheckReturn
    欣萌
    Posted @ 2010-04-06 10:47
    受不了你了。
    你就不能换个颜色写。  回复  更多评论   
  • # re: loki技法(2).CheckReturn
    陈梓瀚(vczh)
    Posted @ 2010-04-06 10:57
    @欣萌
    我认为受不了这种代码的颜色配置是你自己的问题,这可是使用人数最多的。  回复  更多评论   
  • # 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
    @欣萌
    据我个人看法 这个应该于代码测试有关
    如果测试的时候检查了函数返回值则不会有问题
    如果没有检测函数返回值则根据定制规则做处理  回复  更多评论   
  • # 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
    @欲三更
    不一定  回复  更多评论   

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理