forked from ahmidou/SpliceAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added explicit_bool type and cleaned up all the convert-to-booleans i…
…n this list
- Loading branch information
1 parent
56bc5ee
commit 51e88a7
Showing
2 changed files
with
84 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
}; |