Fernando Cacciola wrote:
"Dirk Gerrits" wrote in message arjgo5$o25$[EMAIL PROTECTED]">news:arjgo5$o25$[EMAIL PROTECTED]...>Fernando Cacciola wrote: > >[snip] > > >>void recieve_async_message() >>{ >> optional rcv ; >> while ( !!(rcv = get_async_input()) && !timeout() ) >> output(*rcv); >>} > >[snip] > >Maybe it's a minor point, but I think the !! is really ugly. Have you >considered the safe_bool idiom that's used in some other Boost >libraries? (smart_ptr to name one.) > Yes, I did. safe_bool (among other alternatives) was rejected because of the following (this specific point is explained in detail on the documentation)
Sorry, I only read your post, not the documentation.
void foo() { optional opt = get_some(); bool is_it = opt; } What is the intention of 'is_it'? Does it refer to the 'value' of 'opt', or to its initialized state?
Makes sense. Thanks for the explanation.
The problem is that safe_bool allows for a conversion to bool in certain contexts, but this violates the design premise that there is no implicit conversion from optional to T for the case when T=bool.I guess I'd use if (peek(opt) != 0) or something. It's not that !! is so ugly, but it's not very clear IMHO.
With the current design, the above will fail to compile, requiring the user to disambiguate the meaning by choosing either:
bool is_it_initialized = !!opt;
bool is_it_true = *opt ;
For similar reasons, the following is not supported either:
if ( opt == 0 ) or if ( opt != 0 )
But if you really dislike the syntax, there is an alternative spelling:
if ( peek(opt) ) ....
peek() returns a pointer to the optional value if it is initialized, or NULL if it isn't. Such a pointer can be directly used in a boolean context with the expected meaning.
Dirk Gerrits
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost