0

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 implements the safe-bool idiom, it should enable the following constructs:

  • if (test) { /* test is valid */ }
  • if (!test) { /* test is not valid */ }

...and disallow the following constructs:

  • test << 1;
  • int i = test;
  • delete test;
  • if (test1 == test2) {}
  • if (test != test2) {}
freitass
  • 6,542
  • 5
  • 40
  • 44
  • 1
    Incidentally, with `c++11` just have an `explicit operator bool()` which can't be converted without you saying so except where it is "*contextually converted to `bool`*" (i.e. `if`, `?:`, `&&`, `||`). – BoBTFish Jun 06 '13 at 13:59
  • The reason I want to test the safe-bool idiom is that I want to apply it to a shared_ptr's class of my own. Moreover, the reason why I implementing my own version of shared_ptr is that, because my compiler is really old, the standard library version is not available and the boost one is not compatible with the eld of the compiler. – freitass Jun 06 '13 at 18:02
  • I guess it's a matter of opinion whether `test1 == test2` should be supported. It's after all equal to `(test1 && test2) || (!test1 && !test2)`, which is allowed. IMO, a safe bool is a type which behaves purely as a boolean, and which therefore cannot be used in any other context. Comparison of booleans is very well defined mathematically. – MSalters Jun 07 '13 at 00:18
  • @MSalters: it is less a matter of taste (or opinion) and more a matter of what are you modelling. The object you are modelling may have a different meaning for the comparison operators. Moreover, I disagree with you that `operator==` and `operator&&` are in the same level. In `(test1 && test2)`, for instance, we could extract the bool values of `test1` and `test2` first and then compare those values using the `operator&&`. I don't see the same logic applying to `operator==` just as well. – freitass Jun 07 '13 at 11:43
  • @freitass: I don't follow you. "Extract" the bool values? If that's possible, then surely you can compare those. Basically I expect "safe bool" to follow all rules of boolean algebra. `True AND False = false` is widely accepted. It requires `AND` as well as equality to be defined on booleans. – MSalters Jun 09 '13 at 00:22
  • @MSalters: With "extract the bool values" I mean convert the type to bool using the "safe bool" idiom. If we implement the `operator==` as a "boolean equality", what happens to the "conventional equality"? It is not intuitive (at least for me) interpreting `test1 == test2` as "`test1` is as valid as `test2`" or "(`test1` AND `test2` are valid) OR (`test1` AND `test2` are invalid)". Is that what you are saying? – freitass Jun 10 '13 at 12:35
  • It is also wise to notice that `test1 != test2` is another way to write `test1 XOR test2`. – MSalters Jun 10 '13 at 19:01
  • Allow me to repeat the other concert of mine: what happens to the concept of equality between two instances of a type implementing the "safe bool"? (The type bool do have only two states, but other types implementing "safe bool" may assume more than those two states). – freitass Jun 10 '13 at 19:41

0 Answers0