safe_bool.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 #ifndef LIBMESH_SAFE_BOOL_H
19 #define LIBMESH_SAFE_BOOL_H
20 
21 namespace libMesh
22 {
23 
45 {
46 public:
47  typedef void (safe_bool_base::*bool_type)() const;
49 protected:
50 
53  safe_bool_base & operator=(const safe_bool_base &) {return *this;}
55 };
56 
57 
58 
59 template <typename T>
60 class safe_bool : private safe_bool_base
61 {
62  // private or protected inheritance is very important here as it triggers the
63  // access control violation in main.
64 public:
65  operator bool_type() const
66  {
67  return (static_cast<const T *>(this))->boolean_test()
69  }
70 protected:
72 };
73 
74 
75 
76 // Equality comparison operators between safe_bool<T> and regular bool
77 template <typename T>
78 bool operator==(const safe_bool<T> & lhs, bool b)
79 {
80  return b == static_cast<bool>(lhs);
81 }
82 
83 template <typename T>
84 bool operator==(bool b, const safe_bool<T> & rhs)
85 {
86  return b == static_cast<bool>(rhs);
87 }
88 
89 
90 
91 // Disallow equality comparison operators between safe_bool<T> and safe_bool<U>
92 template <typename T, typename U>
93 bool operator==(const safe_bool<T> & lhs,
94  const safe_bool<U> & /*rhs*/)
95 {
97  return false;
98 }
99 
100 template <typename T,typename U>
101 bool operator!=(const safe_bool<T> & lhs,
102  const safe_bool<U> & /*rhs*/)
103 {
105  return false;
106 }
107 
108 } // namespace libMesh
109 
110 #endif // LIBMESH_SAFE_BOOL_H
void this_type_does_not_support_comparisons() const
Definition: safe_bool.h:48
bool operator!=(const OrderWrapper &lhs, const OrderWrapper &rhs)
Definition: fe_type.h:95
safe_bool_base(const safe_bool_base &)
Definition: safe_bool.h:52
bool operator==(const OrderWrapper &lhs, const OrderWrapper &rhs)
Definition: fe_type.h:94
void(safe_bool_base::* bool_type)() const
Definition: safe_bool.h:47
safe_bool_base & operator=(const safe_bool_base &)
Definition: safe_bool.h:53