根据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库我想写多篇分析其库的短小精悍的例子