Is there a safe bool idiom helper in boost? [closed]
Asked Answered
B

2

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.

Befuddle answered 30/7, 2011 at 14:52 Comment(5)
My, statistics are really made up on the spot.Mortmain
@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.Boutonniere
This "not a real question" had a real answer -- maybe it was a real question after all... ;-]Covenanter
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...Flak
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.Flak
G
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.

Germane answered 30/7, 2011 at 19:14 Comment(1)
Sadly, bool_testable<> was removed from the library, apparently without replacement.Adolphadolphe
F
-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.,

Flak answered 30/7, 2011 at 15:16 Comment(7)
Explicit conversion operators are only available in C++0x.Verdie
@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.Dentilabial
@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.Verdie
@interjay: Agreed, it's not the safe bool idiom, but it is what Alf is suggesting instead.Dentilabial
@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.Flak
@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.Verdie
@interjay: stop trolling, please. you make three untrue assertions. that is very rude. in addition you first comment, while technically true, implied an untruth.Flak

© 2022 - 2024 — McMap. All rights reserved.