The Artima Developer Community
Sponsored Link

Articles Forum
The Safe Bool Idiom

17 replies on 2 pages. Most recent reply: Oct 13, 2010 12:04 PM by Joe Mucchiello

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 17 replies on 2 pages [ « | 1 2 ]
Ian Crawford

Posts: 3
Nickname: codic
Registered: Aug, 2004

Re: The Safe Bool Idiom Posted: Sep 3, 2004 6:54 PM
Reply to this message Reply
> Have you looked at Java since 1.0? The nio package
> provides access to using native select() or poll()
> implementations on socket and file streams.

Hey, lookie there! I googled for this a couple months ago and found nothing (except a few people saying it couldn't be done). Must have been using crappy search criteria.

> But, that is more of that confusing syntax stuff that
> doesn't provide the user with any good sub-conscience
> glue. Making users type obj1.equals( obj2 ) is not a bad
> thing either...

I wouldn't call the "address of" operator confusing, but it's all a matter of opinion.

> Would be
> interesting to see how that would be implemented without
> == being assigned to a member method to begin with.

The compiler writers have managed to do it with implicitly defined operator= and copy constructors. I don't think it's too much of a stretch to apply similar member traversal logic to equality comparisons, but aparently some one did. Perhaps the answer lies in the ARM.

Well, shame on me for not knowing my tools. I obviously don't know either language well enough to effectively argue the finer points of either one in a public forum.

But I do know that you use the right tool for the right job. Sometimes the right tool is C++ (like it or not), and articles like this make better programmers out of all of us.


Victor Rachels

Posts: 1
Nickname: sekmu
Registered: Mar, 2009

Re: The Safe Bool Idiom Posted: Mar 30, 2009 8:19 PM
Reply to this message Reply
A word of warning, and a question for if anyone has encountered and/or has a solution for this issue (other than the obvious "don't allow this code").

If you (already) have an explicit conversion to a pointer type other than bool_type, the compiler may very well choose the non-bool_type, resulting in unexpected behavior.

example crashy:

class Testable
Thing *mData;

typedef void (xOGModelRefPtr::*bool_type)() const;
void type_does_not_support_comparisons() const {}
operator bool_type() const
return (mData && mData->IsValid()) ? Testable::type_does_not_support_comparisons : NULL;

Testable() : mData(NULL) {}
operator Thing*() const { return mData->mPtr; }

void fn(Thing *);
void main()
Testable t;
if(t) // crash on access of null mData->

Joe Mucchiello

Posts: 1
Nickname: joem
Registered: Oct, 2010

Re: The Safe Bool Idiom Posted: Oct 13, 2010 12:04 PM
Reply to this message Reply
Is this code copyright restricted? Is there a license for reuse of this code?

Flat View: This topic has 17 replies on 2 pages [ « | 1  2 ]
Topic: Where Do You Want to Be in Five Years? Previous Topic   Next Topic Topic: What's New in Scala 2.8: Named and Default Parameters

Sponsored Links


Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use