Ticket #1077 (closed enhancement: fixed)
[PATCH] Add bool operator conversion to CmpPtr
| Reported by: | jdm | Owned by: | leper |
|---|---|---|---|
| Priority: | Nice to Have | Milestone: | Alpha 9 |
| Component: | Core engine | Keywords: | patch |
| Cc: |
Description
Consider the elegance of |if (cmpVisual)| instead of |if (!cmpVisual.null())|.
Attachments
Change History
comment:1 follow-up: ↓ 2 Changed 4 months ago by leper
- Keywords patch review added
- Summary changed from Add bool operator conversion to CmpPtr to [PATCH] Add bool operator conversion to CmpPtr
Changed 4 months ago by leper
- Attachment #1077-2012-01-29.patch added
version with updated copyright (year)
comment:2 in reply to: ↑ 1 ; follow-up: ↓ 4 Changed 4 months ago by historic_bruno
Another argument in favor of this change: it would match the syntax we use in JS, which doesn't require checking null().
Replying to leper:
I implemented this but as I am unsure if this approach is safe (as in those this apply here http://www.artima.com/cppsource/safebool.html ?)
Yes, the concerns in that article would apply if we implemented operator bool and someone tried to do the following: if (cmpFoo == cmpBar) ... which would compile but give unintended results. If it's not too difficult, maybe we should implement safe bool? On the one hand we could say we'll review code changes and know better than to try an unsafe bool operation, but then some patches are quite long and one small bug like that could slip through and be difficult to track down.
comment:3 Changed 4 months ago by leper
- Keywords review removed
- Owner set to leper
- Status changed from new to assigned
I'm working on implementing safe bool (resolved the problem of expected type specifier before bool_type by moving the private: above the public declarations).
comment:4 in reply to: ↑ 2 Changed 4 months ago by leper
Replying to historic_bruno:
If it's not too difficult, maybe we should implement safe bool? On the one hand we could say we'll review code changes and know better than to try an unsafe bool operation, but then some patches are quite long and one small bug like that could slip through and be difficult to track down.
Implemented safe bool. == and != comparisions result in a compiler error and a warning that rhs is unused, so maybe replace it with UNUSED(rhs).
comment:5 Changed 4 months ago by leper
- Keywords patch, review added; patch removed
Just stumbled across source/tools/atlas/AtlasObject/AtlasObject.h and noticed that it overloads operator bool but isn't using safe bool. Is this worth fixing (maybe a ticket of its own)?
comment:6 Changed 4 months ago by historic_bruno
Thanks for responding so quickly :) It looks great to me, and I agree about AtlasObject, you could even fix it in this patch if you want, to avoid the wait of having another one reviewed.
Changed 4 months ago by leper
- Attachment #1077-2012-02-04.patch added
added safe bool to AtlasObject.h
comment:7 Changed 4 months ago by leper
No problem ;) Fixed AtlasObject. I also did a quick grep over the source tree and couldn't find any matches for operator bool or operator! in the sources (except some in the libraries but I assume they are of no concern).

I implemented this but as I am unsure if this approach is safe (as in those this apply here http://www.artima.com/cppsource/safebool.html ?) can someone more knowledgable in C++ review this?
I removed null() completely in this patch. Maybe it would be good to leave it in but to make it cause a compiler warning.