File: CodePitfalls.doc

package info (click to toggle)
libzypp 17.38.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 27,744 kB
  • sloc: cpp: 132,661; xml: 2,587; sh: 518; python: 266; makefile: 27
file content (60 lines) | stat: -rw-r--r-- 1,484 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
namespace zypp
{
/** \page CodePitfalls Code Pitfalls - Frequently made mistakes

\section TriBoolCompare Comparing TriBool values

  Comparing two \ref TriBool values is not as easy as it might look like,
  because the \c TriBool::operator== and \c TriBool::operator!= return
  a \ref TriBool.

  For example  is <tt>(indeterminate==indeterminate)</tt> not \c true, but
  \c indeterminate. That's why the following snippet does not do what the
  author expected:

  \code
    // buggy option class
    struct Option
    {
      public:
        Option()
          : _value( indeterminate )
        {}

        bool get() const
        { return ( _value == indeterminate ) ? true : bool(_value); }

        void set( bool new_r )
        { _value = new_r; }

      private:
        tribool _value;
    };
  \endcode

  You find that \c get() returns \c false as long as the option is unset,
  and not \c true as the code suggests. That's because <tt>(_value==indeterminate)</tt>
  returns \c indeterminate.

  \note Always use \c indeterminate(_value) to test whether a TriBools value is \c indeterminate:

  \code
        bool get() const
        { return indeterminate( _value ) ? true : bool(_value); }
  \endcode

  \code
        tribool _value;
        ...
        if ( indeterminate( _value ) )
        { ... }              // indeterminate
        else if ( _value )
        { ... }              // true
        else
        { ... }              // false
  \endcode
<HR>


*/
}