16

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
  • 3
    My, statistics are really made up on the spot. – Mob Jul 30 '11 at 15:00
  • 1
    +1 to counter silly drive-by downvoter. perhaps downvoter was one who does not know what "safe bool idiom" is. then, if one does not know what a "car" is, one should not downvote a question searching for a "car", but instead perhaps post one's own question "wtf. is a CAR"? – Cheers and hth. - Alf Jul 30 '11 at 15:17
  • 6
    @Alf: What about this question could be considered showing research effort? It's two sentences, and the first one is a non-sequitur of dubious accuracy. You may not like the down-voting of the question, but that's a legitimate, reasonable choice considering its phrasing. – Nicol Bolas Jul 30 '11 at 17:03
  • 4
    This "not a real question" had a real answer -- maybe it was a real question after all... ;-] – ildjarn Jul 31 '11 at 20:03
  • I see that someone has added needed vote to close as "not real question" AFTER the op had selected an answer as solution, and with that answer giving exactly what was asked (instead of advice), which seems to contradixt the "cannot be reasonably anaswered". How is it possible to vote that way after the answer? I am not amazed, however... – Cheers and hth. - Alf Aug 01 '11 at 07:23
  • 1
    well, to clarify that comment that i'm not amazed: it has happened before on SO, an uncountable number of times. there is no explanation if one assumes that these people are seriously trying to make SO a good answer and question site. so i am pretty sure that these are children playing social games. – Cheers and hth. - Alf Aug 01 '11 at 08:18

2 Answers2

11

bool_testable<> in Boost.Operators looks promising.

The reference mentions that:

bool_testable provides the antithesis of operator bool, such that the expression if (!p) is valid, whilst also making operator bool safer by preventing accidental conversions to integer types. ... bool_testable<> prevents these accidental conversions by declaring a private conversion operator to signed char, and not defining the body.

UncleBens
  • 40,819
  • 6
  • 57
  • 90
  • 1
    Sadly, [`bool_testable<>` was removed](http://stackoverflow.com/questions/10490675/) from the library, apparently without replacement. – Björn Pollex Apr 04 '13 at 09:28
-1

The safest thing to do is to not have implicit conversion to bool.

Make that conversion explicit.

Then the name, if chosen well, can also help people understand what it's all about.

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 3
    Explicit conversion operators are only available in C++0x. – interjay Jul 30 '11 at 15:20
  • 4
    @interjay: there are two ways to make the conversion explicit - in C++0x, an explicit conversion operator, or in C++03 a member function `bool convert_to_bool();`. Or, to follow the last part of Alf's advance, a function with a better name that indicates what the result actually means. – Steve Jessop Jul 30 '11 at 17:58
  • 1
    @Steve Jessop: Obviously a separate function can be used. But that wouldn't be the safe bool idiom. The idiom is making a `bool_type` typedef from another type (usually a member pointer) and providing an `operator bool_type`. – interjay Jul 30 '11 at 18:09
  • 1
    @interjay: Agreed, it's not the safe bool idiom, but it is what Alf is suggesting instead. – Steve Jessop Jul 30 '11 at 18:13
  • @interjay: I just saw these comments. You seem to not understand what I write, at all. I apologize for not being clear enough for you. Wait, on second thought, I don't apologize. I was clear enough. – Cheers and hth. - Alf Jul 31 '11 at 04:53
  • @anonymous drive-by downvoter: please explain your reasons for downvoting, so that others can see that they're invalid. – Cheers and hth. - Alf Jul 31 '11 at 04:57
  • 1
    @Alf: Your answer is both unclear and doesn't answer the question at all. Funny how you make offsensive comments above to people who don't know what the safe bool idiom is, when your answer doesn't indicate that you know it yourself. – interjay Jul 31 '11 at 14:43
  • @interjay: stop trolling, please. you make three untrue assertions. that is very rude. in addition you first comment, while technically true, implied an untruth. – Cheers and hth. - Alf Aug 01 '11 at 07:12