forked from ahmidou/SpliceAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
explicit_bool.h
37 lines (34 loc) · 926 Bytes
/
explicit_bool.h
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
//
// This template class allows for defining explicit boolean
// version operators without C++0x
//
// This code is taken (mostly) verbatim from
// http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool
// modified so the actual conversion operators is no hidden on the base class
//
// Usage is like so
//
//
// class YourClass :
// public safe_bool <YourClass> // CRTP idiom
// {
// public:
// // Define boolean conversion
// operator explicit_bool::type() const {
// return explicit_bool::get(SomeLogicHere());
// }
// };
//
// This will allow
// if (yourClass)
// but not
// int var = yourClass;
#pragma once
class explicit_bool {
public:
typedef void (explicit_bool::*type)() const;
void this_type_does_not_support_comparisons() const {}
static type get(bool condition) {
return condition ? &explicit_bool::this_type_does_not_support_comparisons : 0;
}
};