Questions tagged [safe-bool-idiom]

Validate objects in a boolean context without harmful side effects caused by implicit conversions.

15 questions
185
votes
2 answers

Is the safe-bool idiom obsolete in C++11?

This answer of @R. Martinho Fernandes shows, that the safe-bool idiom is apperently deprecated in C++11, as it can be replaced by a simple explicit operator bool() const; according to the standard quote in the answer §4 [conv] p3: An expression e…
Xeo
  • 129,499
  • 52
  • 291
  • 397
42
votes
4 answers

C++ safe bool wrapper

I'm trying to design a bool wrapper struct applying the safe bool idiom. The classic implementation to solve this is pretty trivial: the skeleton could be something like this: struct Bool final { Bool() = default; Bool(bool value) :…
Stefano Azzalini
  • 1,027
  • 12
  • 21
36
votes
8 answers

Are there cases where a typedef is absolutely necessary?

Consider the following excerpt from the safe bool idiom: typedef void (Testable::*bool_type)() const; operator bool_type() const; Is it possible to declare the conversion function without the typedef? The following does not compile: operator (void…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
16
votes
2 answers

Is there a safe bool idiom helper in boost?

25% of programmers work time is spended by checking if the required code already exist. I'm searching for a base class for implementing the safe bool idiom.
Mythli
  • 5,995
  • 2
  • 24
  • 31
13
votes
2 answers

Safe bool idiom in boost?

Does the boost library provide an implementation of a safe bool idiom, so that I could derive my class from it? If yes - where is it? If no - what are my alternatives beyond implementing it myself? I found the following similar question: " Is there…
CygnusX1
  • 20,968
  • 5
  • 65
  • 109
9
votes
2 answers

const-correctness and the safe bool idiom

I have another question related to the safe bool idiom: typedef void (Testable::*bool_type)() const; // const necessary? void this_type_does_not_support_comparisons() const {} // const necessary? operator bool_type() const { …
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
7
votes
2 answers

Incompatibilities between safe bool idiom and explicit operator bool

I'm thinking of replacing all the instances of safe bool idiom by explicit operator bool in code which already uses C++11 features (so the fact that older compilers don't recognized explicit conversion operators will not matter), so I'd like to know…
user784668
5
votes
3 answers

Weird compiler error and template inheritance

Could someone explain me why this code: class safe_bool_base { //13 protected: typedef void (safe_bool_base::*bool_type)() const; void this_type_does_not_support_comparisons() const {} //18 safe_bool_base() {} …
ereOn
  • 53,676
  • 39
  • 161
  • 238
4
votes
1 answer

How does "contextual conversion" with `&&` and `||` operators work in conjunction with user-defined operator overloads?

From @Xeo's excellent c++-faq question: Is the safe-bool idiom obsolete in C++11? I learned that the safe bool idiom is no longer needed, because an explicit user-defined conversion to bool will be automatically invoked in contexts where the safe…
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
3
votes
1 answer

Protected member access error in template specialization

class safe_bool_base { protected: void this_type_does_not_support_comparisons() const {} }; template class safe_bool : public safe_bool_base { public: void func() { &safe_bool::this_type_does_not_support_comparisons; …
2
votes
1 answer

Was boost::bool_testable<> relocated or removed?

I'm trying to leverage boost::bool_testable<> (from Boost.Operators) to implement the safe bool idiom for a class, but the most recent version of the library (1.49 as of this post) doesn't seem to have it anymore. Where did it go? Is there a better…
fbrereto
  • 35,429
  • 19
  • 126
  • 178
1
vote
2 answers

C++ safe boolean idiom cannot compile with Visual C++ 10(2010)

Hey guys, I have derived my class from the C++ safe bool idiom class from this page : The Safe Bool Idiom by Bjorn Karlsson class Element : public safe_bool<> { public: bool Exists() const; // boolean_test() is a safe_bool method bool…
ShawnWong
  • 11
  • 1
0
votes
1 answer

How does the safe bool idiom bool_type (and the safe bool idiom) work?

I was pointed to the 'safe bool idiom', and after trying to decipher what is going on (the explanation supplied on the site was not sufficient enough to grant me understanding of why it works), I decided to try to take the following code apart and…
SE Does Not Like Dissent
  • 1,767
  • 3
  • 16
  • 36
0
votes
2 answers

Safe bool multiple conversions ambiguity

I have to implement safe bool idiom in order to support compilers that do not have explicit keyword (MSVC 2012 for example). The class that should be checkable for bool is modelling a pointer to many classes and therefore it should be convertible to…
svv
  • 436
  • 2
  • 12
0
votes
0 answers

Unit testing the safe-bool idiom

To which unit tests should a class be submitted to guarantee it has all the properties required by the C++ safe-bool idiom? Trying to name all of them (extracted from The Safe Bool Idiom), for an instance test of a class Test which supposedly…
freitass
  • 6,542
  • 5
  • 40
  • 44