|
Boost : |
Subject: [boost] [optional] little addition
From: Arno Schödl (aschoedl_at_[hidden])
Date: 2009-02-27 04:08:23
I just started using boost::optional, and this proved to be generically useful, but I believe is currently not in boost::optional:
template< class T, class Tcompatible >
bool optional_assign( T& t, boost::optional<Tcompatible> const& optt ) {
if( optt ) {
t=*optt;
return true;
} else {
return false;
}
}
so you can write
boost::optional< TMyValue > TryToGetT();
T t;
if( !optional_assign( t, TryToGetT () ) {
... other way to get t ...
}
instead of the more verbose:
boost::optional< TMyValue > TryToGetT();
T t;
boost::optional< TMyValue > const& optt=TryToGetT();
if( optt ) {
t=*optt;
} else {
... other way to get t ...
}
get_optional_value_or is similar, but the alternative must be an expression and is eagerly evaluated, both of which may be undesirable.
Arno
-- Dr. Arno Schoedl · aschoedl_at_[hidden] Technical Director think-cell Software GmbH · Invalidenstr. 34 · 10115 Berlin, Germany http://www.think-cell.com · phone +49-30-666473-10 · toll-free (US) +1-800-891-8091 Directors: Dr. Markus Hannebauer, Dr. Arno Schoedl · Amtsgericht Charlottenburg, HRB 85229
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk